how to store django media files to spaces?

Posted March 13, 2020 7.5k views
Block Storage

MEDIAURL =’/media/’

how to link to my spaces so that files will be uploaded to spaces not the server.

staticfiles are working as stored on the spaces. but i dont know how to upload media files to storages.

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

Just got this working last night, this is how I did it.

Add a file in the same directoy as This way you don’t need to import the module in your file.

from storages.backends.s3boto3 import S3Boto3Storage

class StaticStorage(S3Boto3Storage):
    bucket_name = '<your_space_name>'
    location = 'static'

class MediaStorage(S3Boto3Storage):
    bucket_name = '<your_space_name>'
    location = 'media'

I have my settings set up like Two Scoops of Django. Sorry I don’t know how to do the fancy file structure markup.


In your production settings, i.e, or, however you have it configured, you need the following:

  • SECRET_KEY at top of file above import statements.
AWS_ACCESS_KEY_ID = '<your_key>' 
AWS_SECRET_ACCESS_KEY = '<your_secret_key>'

AWS_STORAGE_BUCKET_NAME = '<your_space_name>'
AWS_S3_ENDPOINT_URL = '<your_endpoint_url>'
# I enabled the CDN, so you get a custom domain. Use the end point in the AWS_S3_CUSTOM_DOMAIN setting. 
AWS_S3_CUSTOM_DOMAIN = '<your_custom_domain_url>'
    'CacheControl': 'max-age=86400',

AWS_DEFAULT_ACL = 'public-read'

STATICFILES_STORAGE = 'custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

# Use AWS_S3_ENDPOINT_URL here if you haven't enabled the CDN and got a custom domain. 
STATIC_URL = '{}/{}/'.format(AWS_S3_CUSTOM_DOMAIN, 'static')
STATIC_ROOT = 'static/'

MEDIA_URL = '{}/{}/'.format(AWS_S3_CUSTOM_DOMAIN, 'media')
MEDIA_ROOT = 'media/'

Then run:

python collectstatic

That will copy all your static files to a folder like so <your_space_name>/static/

Go to your admin interface or a page with file upload, and upload a test file. It should upload to <your_space_name>/media/

One thing that did happen with mine was that it was adding in another <your_space_name> to the endpoint of the static and media files. So it looked like this:


To get the url that Django rendered the same as the File url in the Space, I had to manually add in another /<your_space_name/ to the custom domain endpoint, i.e AWS_S3_CUSTOM_DOMAIN. So it looked like this:

AWS_S3_CUSTOM_DOMAIN = '<your_custom_domain_url>/<your_space_name>'

The following resources are helpful and contain everything you need to get it working:

  • Hi sir,

    Thank you for that great help. The problem with the <space-name> at the end of your AWSS3CUSTOM_DOMAIN comes from the url endpoint.

    As you have AWSBUCKETNAME=<your-bucket-name> you should set up your AWSENDPOINTURL to “https://<your-space-region>”.

    The bucket name will be added by itself.

    Also you should add AWSS3ADDRESSING_STYLE = ‘virtual’ to your settings.

    Best regards,

  • This has been my go-to tutorial for a while now ;)

    Just a quick note on my side: if you happen to use Spaces with Heroku, bear in mind that django-heroku.settings(locals()) is gonna overwrite most of your static files configuration, so you might want to comment it our / put it in conditional block before trying out your static config.

how to link to my spaces so that files will be uploaded to spaces not the server.

If I understand you correctly, you want to upload the files programmatically to Spaces instead of the Droplet that you are using. You can use the Digital Ocean API to perform these actions, here is the reference API to upload an object (using HTTP PUT request). API exposes other functions that can be used to perform other options (delete, query, etc.).

Check this how-to guide about the Spaces API to learn about what is possible with Spaces.