6

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.

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

4 Comments

This is awesome! Thanks man! The only thing that you might want to ass is, that as this approach ised Boto, so anyone using this approach will have to install Boto too. I did have another question for you though. how do I add a access control policy = public-read to it? I tried adding acl='public-read' in settings.py but that did not seem to work
Rather I just want google docs viewer to be able to Access the files I upload. Is there a way to just make that happen?
maybe aws.amazon.com/s3/faqs/… should help?
the problem was way basic than that. All I had to do was user urlencode :) sorry for the trouble.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.