How to upload a file to S3 and make it public using boto3?

PythonAmazon S3BotoBoto3

Python Problem Overview


I am able to upload an image file using:

s3 = session.resource('s3')
bucket = s3.Bucket(S3_BUCKET)
bucket.upload_file(file, key)

However, I want to make the file public too. I tried looking up for some functions to set ACL for the file but seems like boto3 have changes their API and removed some functions. Is there a way to do it in the latest release of boto3?

Python Solutions


Solution 1 - Python

To upload and set permission to publicly-readable in one step, you can use:

bucket.upload_file(file, key, ExtraArgs={'ACL':'public-read'})

See https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html#the-extraargs-parameter

Solution 2 - Python

I was able to do it using objectAcl API:

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

For details: http://boto3.readthedocs.io/en/latest/reference/services/s3.html#objectacl

Solution 3 - Python

Adi's way works. However, if you were like me, you might have run into an access denied issue. This is normally caused by broken permissions of the user. I fixed it by adding the following to the Action array:

"s3:GetObjectAcl",
"s3:PutObjectAcl"

Solution 4 - Python

In the recent versions of boto, ACL is available as a regular parameter - both when using the S3 client and resource, it seems. You can just specify ACL="public_read" without having to wrap it with ExtraParams or using ObjectAcl API.

Solution 5 - Python

Set the ACL="public-read" as mentioned above.

Also, make sure your bucket policy Resource line has both the bare arn and /* arn formats. Not having them both can cause strange permissions problems.

...
"Resource": ["arn:aws:s3:::my_bucket/*", "arn:aws:s3:::my_bucket"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdiView Question on Stackoverflow
Solution 1 - PythonBill BakerView Answer on Stackoverflow
Solution 2 - PythonAdiView Answer on Stackoverflow
Solution 3 - PythonRohan BagchiView Answer on Stackoverflow
Solution 4 - PythonCzyzbyView Answer on Stackoverflow
Solution 5 - Pythonjoel3000View Answer on Stackoverflow