how to store django media files to spaces?

MEDIA_URL =‘/media/’ MEDIA_ROOT=os.path.join(BASE_DIR,‘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.

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.

config —>settings ------> ------> ------>

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:

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.