Search code examples
amazon-web-servicesamazon-s3amazon-iampublicpolicy

How do you allow granting public read access to objects uploaded to AWS S3?


I have created a policy that allows access to a single S3 bucket in my account. I then created a group that has only this policy and a user that is part of that group.

The user can view, delete and upload files to the bucket, as expected. However, the user does not seem to be able to grant public read access to uploaded files.

enter image description here

When the Grant public read access to this object(s) option is selected, the upload fails.

The bucket is hosting a static website and I want to allow the frontend developer to upload files and make them public.

The policy for the user role is below:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "s3:*"
        ],
        "Resource": "arn:aws:s3:::my-bucket"
    },
    {
        "Effect": "Allow",
        "Action": [
            "s3:*"
        ],
        "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

This is what happens when the IAM user tries to grant public access to the uploaded file: enter image description here

The proxy error seems unrelated, but essentially the upload is stuck and nothing happens. If they don't select the Grant public access option, the upload goes through immediately (despite the proxy error showing up as well).


Solution

  • To reproduce your situation, I did the following:

    • Created a new Amazon S3 bucket with default settings (Block Public Access = On)
    • Created an IAM User (with no policies attached)
    • Created an IAM Group (with no policies attached)
    • Added the IAM User to the IAM Group
    • Attached your policy (from the Question) to the IAM Group (updating the bucket name) as an inline policy
    • Logged into the Amazon S3 management console as the new IAM User

    At this point, the user received an Access Denied error because they were not permitted to list all Amazon S3 buckets. Thus, the console was not usable.

    Instead, I ran this AWS CLI command:

    aws s3 cp foo.txt s3://new-bucket/ --acl public-read
    

    The result was:

    An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

    However, the operation succeeded with:

    aws s3 cp foo.txt s3://new-bucket/
    

    This means that the --acl is the component that was denied.

    I then went to Block Public Access for the bucket and turned OFF the option called "Block public access to buckets and objects granted through new access control lists (ACLs)". My settings were:

    Block Public Access

    I then ran this command again:

    aws s3 cp foo.txt s3://new-bucket/ --acl public-read
    

    It worked!

    To verify this, I went back into Block Public Access and turned ON all options (via the top checkbox). I re-ran the command and it was Access Denied again, confirming that the cause was the Block Public Access setting.

    Bottom line: Turn off the first Block Public Access setting.