Created
September 15, 2023 13:27
-
-
Save johnpolacek/fbb0f7ccc32c861d6544b0030ec71c66 to your computer and use it in GitHub Desktop.
Create a Public S3 Bucket via AWS SDK for Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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