How to make all Objects in AWS S3 bucket public by default?

后端 未结 3 1044
醉酒成梦
醉酒成梦 2020-11-29 15:34

I am using a PHP library to upload a file to my bucket. I have set the ACL to public-read-write and it works fine but the file is still private.

I

相关标签:
3条回答
  • 2020-11-29 15:54

    Go to http://awspolicygen.s3.amazonaws.com/policygen.html Fill in the details such as: enter image description here In Action select "GetObject" Select "Add Statement" Then select "Generate Policy"

    Copy the text example:

    {
      "Id": "Policy1397632521960",
      "Statement": [
        {
          "Sid": "Stmt1397633323327",
          "Action": [
            "s3:GetObject"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::bucketnm/*",
          "Principal": {
            "AWS": [
              "*"
            ]
          }
        }
      ]
    }
    

    Now go to your AWS S3 console, At the bucket level, click on Properties, Expand Permissions, then Select Add bucket policy. Paste the above generated code into the editor and hit save.

    All your items in the bucket will be public by default.

    0 讨论(0)
  • 2020-11-29 15:56

    My problem was slightly different, but since this question is on the top of google search I'll leave my solution, maybe it'll help somebody.

    I already had had full access to S3 bucket before, but one day it just started to return Access Denied to all my files. The solution was straightforward simple.

    1. Go to Services - S3
    2. Click on your S3 bucket
    3. Switch to Permissions tab, then go to Bucket Policy tab
    4. And click the Save button.

    It should reassign permission on all your files.

    Anyway, here is full bucket policy that allows makes all object public

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowPublicRead",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "*"
                },
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::enter-here-your-media-bucket-name/*"
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-29 16:00

    If you want to make all objects public by default, the simplest way is to do it trough a Bucket Policy instead of Access Control Lists (ACLs) defined on each individual object.

    You can use the AWS Policy Generator to generate a bucket policy for your bucket.

    For example, the following policy will allow anyone to read every object in your S3 bucket (just replace <bucket-name> with the name of your bucket):

    {
      "Id": "Policy1380877762691",
      "Statement": [
        {
          "Sid": "Stmt1380877761162",
          "Action": [
            "s3:GetObject"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::<bucket-name>/*",
          "Principal": {
            "AWS": [
              "*"
            ]
          }
        }
      ]
    }
    

    The Bucket Policy contains a list of Statements and each statement has an Effect (either Allow or Deny) for a list of Actions that are performed by Principal (the user) on the specified Resource (identified by an Amazon Resource Name or ARN).

    The Id is just an optional policy id and the Sid is an optional unique statement id.

    For S3 Bucket Policies, the Resource ARNs take the form:

    arn:aws:s3:::<bucket_name>/<key_name>
    

    The above example allows (Effect: Allow) anyone (Principal: *) to access (Action: s3:GetObject) any object in the bucket (Resource: arn:aws:s3:::<bucket-name>/*).

    0 讨论(0)
提交回复
热议问题