Skip to content

Instantly share code, notes, and snippets.

@hancush
Last active March 7, 2018 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hancush/7d8340d79b919fce7f0b8e6103da6e83 to your computer and use it in GitHub Desktop.
Save hancush/7d8340d79b919fce7f0b8e6103da6e83 to your computer and use it in GitHub Desktop.
store django model-related files in s3

Install django-storages:

pip install django-storages

Add to settings:

INSTALLED_APPS = (
    ...
    'storages',
    ...
)

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

Add to private settings:

# Note that you need to make the S3 bucket first.

AWS_STORAGE_BUCKET_NAME = '<bucket_name>'
AWS_ACCESS_KEY_ID = '<key>'
AWS_SECRET_ACCESS_KEY = '<secret>'

Add to model:

def upload_name(instance, filename):
    # see https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.FileField.upload_to
    # return path/to/desired/filename
    pass
    
some_file = models.FileField(max_length=1000, upload_to=upload_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment