Report this

What is the reason for this report?

How to set a file-listing on Digital Ocean spaces to public using boto3 or Python?

Posted on February 2, 2021

I need files I upload to be public. Is there a way to set a default so every file you upload is public? Otherwise, is there a way you can set a file to public using boto3 or a Python library?

Thanks in advance!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

To set a file as public with boto3 for DigitalOcean Spaces, you can set the ACL (Access Control List) while uploading the file to ‘public-read’. This will make the file accessible to the public. Here’s a sample code:

  1. import boto3
  2. s3 = boto3.client('s3',
  3. region_name='nyc3',
  4. endpoint_url='https://nyc3.digitaloceanspaces.com',
  5. aws_access_key_id='YOUR_ACCESS_KEY',
  6. aws_secret_access_key='YOUR_SECRET_KEY'
  7. )
  8. s3.upload_file('FILENAME', 'bucketname', 'objectname', ExtraArgs={'ACL':'public-read'})

However, to set a default policy making all uploaded files public, you would need to set a bucket policy that allows public read access to objects.

Please note that making a bucket public can have serious security implications and should only be done if you are sure this is what you want.

You can find more details about interacting with Spaces using boto3 in our DigitalOcean Spaces API documentation.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.