How to make s3 bucket public in python

时光总嘲笑我的痴心妄想 提交于 2020-01-07 01:55:06

问题


I have an already created bucket in amazon s3. I want to make its content publicly available without any authentication. I have tried from documents of boto

To set a canned ACL for a bucket, use the set_acl method of the Bucket object. The argument passed to this method must be one of the four permissable canned policies named in the list CannedACLStrings contained in acl.py. For example, to make a bucket readable by anyone:

b.set_acl('public-read')

It is not working. I still cant access my files publicly. However setting acl to public-read for individual files is working.

I want to make it public from python as I don't have access to s3 console.

I want to make whole bucket publicly readable.

My code is

    conn = boto.connect_s3(
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key,
        host = 's3.amazonaws.com',
        #is_secure=False,               # uncomment if you are not using ssl
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )
bucket = conn.get_bucket('media_library')
bucket.set_acl('public-read')

回答1:


You will need to create a Bucket Policy, which defines permissions on the bucket as a whole.

See 'Bucket Policy' in: Managing Access to S3 Resources (Access Policy Options)

You can do this via boto in Python with put_bucket_policy():

put_bucket_policy(**kwargs)

Replaces a policy on a bucket. If the bucket already has a policy, the one in this request completely replaces it.

Request Syntax

response = client.put_bucket_policy(Bucket='string', Policy='string')

See Bucket Policy Examples to find a suitable policy.

Here is a policy that makes the whole bucket publicly readable (just insert your own bucket name):

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


来源:https://stackoverflow.com/questions/38196758/how-to-make-s3-bucket-public-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!