Question

how to store django media files to spaces?

Posted March 13, 2020 2.4k views
Block Storage

MEDIAURL =’/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.

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.

×
2 answers

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.

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

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

custom_storages.py

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
——>local.py
——>dev.py
——>prod.py

In your production settings, i.e prod.py, or settings.py, 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>'
AWS_S3_OBJECT_PARAMETERS = {
    '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 manage.py 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:

https://<aws_s3_custom_domain>/<your_space_name>/<your_space_name>/static/css/theme.css

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:

Submit an Answer