Home > OS >  How to set access level as "Public" for all files uploading to an S3 bucket?
How to set access level as "Public" for all files uploading to an S3 bucket?

Time:03-14

I have created an S3 bucket and also an API through the AWS API Gateway to upload images to the bucket. The problem is, when I upload an image, to view that image I need to update the Access control list (ACL) to Public for each image separately. Even though I set everything to the public in the bucket permissions, still I have to update the ACL in each image to access them. How can I set the access level to "Public" for the whole bucket once?

This is my bucket permissions:

Access: Public

Block all public access: Off

Bucket policy:

{
    "Version": "2012-10-17",
    "Id": "Policy1647249671911",
    "Statement": [
        {
            "Sid": "Stmt1647249649218",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::mybucketname"
        }
    ]
}

Access control list (ACL):

enter image description here

CodePudding user response:

Your current policy is highly insecure and allows anyone to do pretty much anything with your bucket, including changing it policy or deleting it.
The correct bucket policy for public, read-only access is:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicRead",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"
            ]
        }
    ]
}
  • Related