How to make s3 bucket public in python

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

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

  

b.set_acl ('public reading')

  

He does not work. I still cannot publish my files. However, setting acl to per public-read

-file works.

I want to make it publicly available from python since I don't have access to the s3 console.

I want the whole bucket to be public.

My code

    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')

      

0


source to share


1 answer


You will need to create a Bucket Policy that defines the permissions for the bucket as a whole.

See "Billing Policy" in: Controlling Access to S3 Resources (Access Policy Settings)

You can do it through boto

in Python with put_bucket_policy()

:

put_bucket_policy (** kwargs)

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

Query syntax

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



See Bucket Policy Examples to find a suitable policy.

Here is the policy that makes the bucket public (just insert your own bucket name):

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

      

+1


source







All Articles