Permalink
Browse files

Add static file serving via S3

  • Loading branch information...
1 parent 4509503 commit dce218b1782c23bf60d8e448e6864f85541b89cb Michael Mior committed Dec 23, 2012
Showing with 71 additions and 18 deletions.
  1. +15 −0 README.markdown
  2. +49 −18 app/settings.py
  3. +4 −0 env.sample
  4. +3 −0 requirements.txt
View
@@ -49,6 +49,21 @@ Initially, and when the schema changes, run `syncdb` and `migrate`.
heroku run python manage.py migrate
You can view your new deployment in your browser via `heroku open`.
+Static file serving will be broken as this requires an S3 account.
+Create a bucket on S3 and add the configuration to your Heroku installation.
+
+ heroku config:add AWS_ACCESS_KEY_ID="<AWS access key>"
+ heroku config:add AWS_SECRET_ACCESS_KEY="<AWS secret>"
+ heroku config:add AWS_STORAGE_BUCKET_NAME="<bucket name>"
+
+
+You can manually run `collectstatic via
+
+ heroku run python manage.py collectstatic --noinput
+
+Optionally, to have `collectstatic` run automatically on every deploy, use
+
+ heroku labs:enable user-env-compile
Dependency management
View
@@ -36,24 +36,55 @@
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
-# Absolute filesystem path to the directory that will hold user-uploaded files.
-# Example: "/home/media/media.lawrence.com/media/"
-MEDIA_ROOT = ''
-
-# URL that handles the media served from MEDIA_ROOT. Make sure to use a
-# trailing slash.
-# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
-MEDIA_URL = ''
-
-# Absolute path to the directory static files should be collected to.
-# Don't put anything in this directory yourself; store your static files
-# in apps' "static/" subdirectories and in STATICFILES_DIRS.
-# Example: "/home/media/media.lawrence.com/static/"
-STATIC_ROOT = ''
-
-# URL prefix for static files.
-# Example: "http://media.lawrence.com/static/"
-STATIC_URL = '/static/'
+# While debugging, use the built-in server's static file serving mechanism.
+# In production, host all files on S3.
+if not DEBUG:
+ # Access information for the S3 bucket
+ AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
+ AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
+ AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
+ AWS_S3_CUSTOM_DOMAIN = AWS_STORAGE_BUCKET_NAME
+
+ # Static files are stored in the bucket at /static
+ # and user-uploaded files are stored at /media
+ STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
+ DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
+ DEFAULT_FILE_STORAGE = 's3_folder_storage.s3.DefaultStorage'
+ DEFAULT_S3_PATH = 'media'
+ STATICFILES_STORAGE = 's3_folder_storage.s3.StaticStorage'
+ STATIC_S3_PATH = 'static'
+ AWS_S3_SECURE_URLS = False
+ AWS_QUERYSTRING_AUTH = False
+
+ # Construct the paths to resources on S3 via
+ # the bucket name and the necessary paths
+ MEDIA_ROOT = '/%s/' % DEFAULT_S3_PATH
+ MEDIA_URL = '//%s.s3.amazonaws.com/%s/' % \
+ (AWS_STORAGE_BUCKET_NAME, DEFAULT_S3_PATH)
+ STATIC_ROOT = '/%s/' % STATIC_S3_PATH
+ STATIC_URL = '//%s.s3.amazonaws.com/%s/' % \
+ (AWS_STORAGE_BUCKET_NAME, STATIC_S3_PATH)
+ ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
+else:
+ # Absolute filesystem path to the directory
+ # that will hold user-uploaded files.
+ # Example: "/home/media/media.lawrence.com/media/"
+ MEDIA_ROOT = ''
+
+ # URL that handles the media served from MEDIA_ROOT. Make sure to use a
+ # trailing slash.
+ # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
+ MEDIA_URL = ''
+
+ # Absolute path to the directory static files should be collected to.
+ # Don't put anything in this directory yourself; store your static files
+ # in apps' "static/" subdirectories and in STATICFILES_DIRS.
+ # Example: "/home/media/media.lawrence.com/static/"
+ STATIC_ROOT = ''
+
+ # URL prefix for static files.
+ # Example: "http://media.lawrence.com/static/"
+ STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
View
@@ -1,3 +1,7 @@
DATABASE_URL="postgres://user:password@localhost/database"
DJANGO_DEBUG=1
DJANGO_SECRET_KEY="RANDOM_STRING"
+
+AWS_ACCESS_KEY_ID=""
+AWS_SECRET_ACCESS_KEY=""
+AWS_STORAGE_BUCKET_NAME=""
View
@@ -1,5 +1,8 @@
-e git+https://github.com/django/django.git@82d4786544e2331b23de468aba875e9600162d76#egg=Django-dev
South==0.7.6
+boto==2.6.0
dj-database-url==0.2.1
+django-s3-folder-storage==0.1
+django-storages==1.1.5
gunicorn==0.16.1
psycopg2==2.4.6

0 comments on commit dce218b

Please sign in to comment.