Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Boto3 and Python How Make a Folder Public

I want to make a folder called img that already exists within my private bucket public. I'm using Boto3. I just want to make this folder public not anything else using a script..

This is how I'm currently connecting to the bucket and how far I have got....

ACCESS_KEY_ID = 'xxxxx'
ACCESS_KEY_SECRET = 'xxxx'
bucket_name = 'mybucket'
sourceDir = "../../docs/buildHTML/html/"
destDir = ''

r = boto3.setup_default_session(region_name='eu-west-1')
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=ACCESS_KEY_SECRET)
bucket = s3.Bucket(bucket_name)

So I have the bucket and this works. How do I now make the folder img that already exists public?

like image 260
Prometheus Avatar asked Feb 09 '23 03:02

Prometheus


1 Answers

You need to add a policy to the bucket, something like this:

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

You can do this through the AWS console, or any of the SDKs. In boto3, I think you do it like this:

bucket = s3.Bucket(bucket_name)

response = bucket.put(
    Policy = '<policy string here>'
)
like image 53
jarmod Avatar answered Feb 12 '23 11:02

jarmod