Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + Heroku + S3

I've been trying to research ways to make AWS S3 work with Heroku for FileField and ImageField uploads. But I've been failing to get it working.

The plan is to

  1. Use FileField to upload some files.
  2. Use ImageField for profile pictures.
  3. Use Sorl Thumbnails to resize those pictures.

I followed this article, but it doesn't seem to work. Is there something that I am missing? I want file uploads, as painless as possible. Is there a better alternative which is more django and heroku friendly than AWS? Any help in this regard would be highly appreciated.

like image 595
Jonathan Avatar asked Oct 07 '12 06:10

Jonathan


1 Answers

Use Django Storages for managing static files on S3. Then follow Heroku Static assets guide when deploying.

First, create a bucket in S3, using either the AWS Console or your favorite tool. Then, modify your settings.py and add the following values:

import os

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = '<YOUR BUCKET NAME>'

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

Notice that we are using environment variables to store the AWS access key and secret key. While we are on this topic, if you are planning to open source the Django application you are deploying, consider also storing your SECRET_KEY in an environment variable.

The above is from here

like image 157
Pratik Mandrekar Avatar answered Oct 14 '22 19:10

Pratik Mandrekar