Image and file upload to s3 django

28 February, 2016
Python Django AWS

So with the last post you now know how to add images to your models and setup a file upload to a specific location. There is more to go.
Now.

We will be using with boto and django-storages to setup this upload which will work for all your image or file uploads across project.

In your requirements.txt file.

boto==2.34.0
django-storages==1.1.8

Your settings.py
 

AWS_QUERYSTRING_AUTH = False
AWS_ACCESS_KEY_ID = 'YOUR_AWS_ACCESS_KEY_ID'
AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_ACCESS_KEY'
AWS_STORAGE_BUCKET_NAME = 'YOUR_BUCKET_NAME'

DEFAULT_FILE_STORAGE = 'path.s3_storage.MediaS3BotoStorage'
Now create a s3_storage.py file in the path and add the following:
from storages.backends.s3boto import S3BotoStorage


HEADERS = {"x-amz-meta-Cache-Control": "max-age=600"}
StaticS3BotoStorage = lambda: S3BotoStorage(location='static', gzip=True,
                                            headers=HEADERS)

Thank you for taking the time to read this post. If you're considering using Digital Ocean, the hosting provider this blog is hosted on, please consider using this link to sign up.