How to upload an object to digital ocean spaces using python boto3 library.

Posted November 27, 2017 19.4k views
DigitalOceanPythonUbuntu 16.04

I am trying to automated some of my task related to digialocean spaces.
but stuck at when i a trying to upload an object to spaces. and i also want to know is there any way to set expiration tag on the object.

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.

Submit an Answer
2 answers

To answer the second question first, Spaces does not currently support “lifecycle” events. So unfortunately you can not use expiration tags at this time.

For the question in the title, uploading an object to Spaces using boto3 requires that you edit the endpoint setting for the session. Here’s a quick example:

import boto3

session = boto3.session.Session()
client = session.client('s3',

client.upload_file('/path/to/file.ext',  # Path to local file
                   'my-space',  # Name of Space
                   'file.ext')  # Name for remote file
  • @asb Thank you for the example, it works perfectly.
    Does Digital Ocean support boto as well? I have been trying to replicate the code above using boto, but without success..

    • Spaces will work with boto as well, though there are differences in the syntax. Here’s an example doing the same as the above:

      import boto
      import boto.s3.connection
      conn = boto.connect_s3(aws_access_key_id='ACCESS_KEY',
      bucket = conn.get_bucket('my-bucket')
      file = bucket.new_key('new-file.ext')

      The key thing here is setting the host to point to the Spaces endpoint.

  • Hi,
    I am trying to use boto3 example but I am getting ‘NoSuchBucket’ error.

    s3.Object(BUCKET_NAME, 'file.ext’).load()
    The above works.

    transfer.uploadfile(path , BUCKETNAME, 'file.ext’) return a 'NoSuchBucket’ error.

  • How to delete the image from space any sample are available?

    i have tried like below:

    client.delete_file(“my-space’, # Name of Space

    But i’m getting error like:

    AttributeError: 'S3’ object has no attribute 'delete_file’

    Please let me know any solution for this issue

Check this out
simple script to help manage your spaces and droplets in python