How to make AWS S3 buckets public through bucket policy


The first step is to create a S3 buckets, here is how we can create one on AWS website through UI:
create S3 bucket

After clicking the “create bucket”, in the following configuration page, give it a name.
Notice that the bucket name should be unique among all AWS buckets, not only unique to your own buckets.
We call it “datasciencebyexample-demo” here.

If we want to make part or all of the contents in this bucket public, unlick the “Block all public access”
option in this step as the following screenshot:
allow public access in S3

Clicking into this new bucket, and let’s create one new folder called “test”:
create folder in S3

Get into the test folder, and upload some files, for example, we uploaded a file called text.txt.
But if we open the url of this just uploaded file in the bucket, you will find the file is still not publicly accessible.

Why is that? The previous step we did actually only make this bucket “Objects can be public”.
We still need to add bucket policy to make all of the bucket or part of bucket contents really public.

Now go back to the bucket level, find Permission tab, and click the Edit button under bucket policy:

If we want to make all objects under this bucket to be public, put the following policy statement in the text box:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::datasciencebyexample-demo/*"
}
]
}

If we only want to make objects under the “test” directory to be public, put the following policy statement in the text box:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::datasciencebyexample-demo/test/*"
}
]
}

After saving the bucket policy, you will find your file urls are now public!


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC