user791793
user791793

Reputation: 701

Making all s3 files in a folder public with boto3

I have a bunch of files in an S3 bucket which all need to be public. However, as new files are added there by my teammates, they forget to set them to public.

So, I'm using a python3 script to make sure all the respective files are made public.

Here is what I have so far:

import boto3
s3 = boto3.resource('s3', region_name='eu-west-1')

## Bucket to use
bucket = s3.Bucket('my_bucket')

## List objects within a given prefix
for obj in bucket.objects.filter(Delimiter='/', Prefix='public_folder/'):
    obj.set_acl('public-read')

When I try the above I get an error:


AttributeError: 's3.ObjectSummary' object has no attribute 'set_acl'

How do I do this?

Upvotes: 0

Views: 1311

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269340

If your goal is to make the whole bucket public (or even just a subdirectory within the bucket), the easier method is to create a Bucket Policy like this example from Bucket policy examples - Amazon Simple Storage Service:

The following example policy grants the s3:GetObject permission to any public anonymous users. This permission allows anyone to read the object data, which is useful for when you configure your bucket as a website and want everyone to be able to read objects in the bucket. Before you use a bucket policy to grant read-only permission to an anonymous user, you must disable block public access settings for your bucket.

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

This will grant public access regardless of the ACL setting on the individual objects.

Upvotes: 1

wolverine
wolverine

Reputation: 84

Get the object acl first and edit it

s3 = boto3.resource('s3')
object_acl = s3.ObjectAcl('bucket_name','object_key')
response = object_acl.put(ACL='public-read')

Upvotes: 0

Related Questions