Skip to content

Instantly share code, notes, and snippets.

@faisalmahmud
Last active August 29, 2015 14:03
Show Gist options
  • Save faisalmahmud/a08386feb25bcf3c013d to your computer and use it in GitHub Desktop.
Save faisalmahmud/a08386feb25bcf3c013d to your computer and use it in GitHub Desktop.
Django settings.py when using django-storages
# s3storages.py
import urlparse
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
def domain(url):
return urlparse.urlparse(url).hostname
class S3StaticStorage(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket'] = settings.AWS_STORAGE_BUCKET_NAME
kwargs['custom_domain'] = domain(settings.STATIC_URL)
super(S3StaticStorage, self).__init__(*args, **kwargs)
class S3MediaStorage(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket'] = settings.AWS_MEDIA_BUCKET_NAME
kwargs['custom_domain'] = domain(settings.MEDIA_URL)
super(S3MediaStorage, self).__init__(*args, **kwargs)
# settings.py
# AWS SETTINGS FOR S3
STATICFILES_STORAGE = 'core.s3storages.S3StaticStorage'
DEFAULT_FILE_STORAGE = 'core.s3storages.S3MediaStorage'
AWS_PRELOAD_METADATA = True
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['S3_STATIC_BUCKET']
AWS_MEDIA_BUCKET_NAME = os.environ['S3_MEDIA_BUCKET']
AWS_S3_SECURE_URLS = True
AWS_QUERYSTRING_AUTH = False
AWS_HEADERS = {
'Expires': 'Thu, 11 Juni 2016 20:00:00 GMT',
'Cache-Control': 'max-age=86400',
}
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = 'http://a1b2c3d4e5f6.cloudfront.net/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = 'http://a1b2c3d4e5f6.cloudfront.net/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment