Skip to content

Instantly share code, notes, and snippets.

@johnpolacek
Created September 15, 2023 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpolacek/fbb0f7ccc32c861d6544b0030ec71c66 to your computer and use it in GitHub Desktop.
Save johnpolacek/fbb0f7ccc32c861d6544b0030ec71c66 to your computer and use it in GitHub Desktop.
Create a Public S3 Bucket via AWS SDK for Node
// Create the bucket
const bucketName = await getBucketName()
const createBucketCommand = new CreateBucketCommand({
Bucket: bucketName,
ObjectOwnership: "ObjectWriter",
})
await s3.send(createBucketCommand)
// Delete the block public access
const deletePublicAccessBlockCommand = new DeletePublicAccessBlockCommand({
Bucket: bucketName,
})
await s3.send(deletePublicAccessBlockCommand)
// Set the public access policy
const bucketPolicy = {
Version: "2012-10-17",
Statement: [
{
Sid: "PublicReadGetObject",
Effect: "Allow",
Principal: "*",
Action: "s3:GetObject",
Resource: `arn:aws:s3:::${bucketName}/*`,
},
],
}
const putBucketPolicyCommand = new PutBucketPolicyCommand({
Bucket: bucketName,
Policy: JSON.stringify(bucketPolicy),
})
await s3.send(putBucketPolicyCommand)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment