Created
February 24, 2014 15:13
-
-
Save jcderr/9190109 to your computer and use it in GitHub Desktop.
Using django-storages to put MEDIA in S3, STATICFILES in Cloudfront
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from storages.backends.s3boto import S3BotoStorage | |
class UserMediaS3Storage(S3BotoStorage): | |
def __init__(self, *args, **kwargs): | |
# if we set settings.AWS_S3_BUCKET, it will interfere with storagres.backends.s3boto.S3BotoStorage | |
# rendering of our {% static %} assets. So, we're leaving that unset and getting our bucket from | |
# another settings var. | |
kwargs['bucket'] = getattr(settings, 'USER_MEDIA_STORAGE_BUCKET') | |
# We had to set AWS_S3_CUSTOM_DOMAIN for CloudFront to work with {% static %} tags. However, this | |
# interferes with rendering the url for a FileField on an object. We'll unset it now so that the | |
# default behavior returns. | |
kwargs['custom_domain'] = None | |
super(UserMediaS3Storage, self).__init__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [ ... snip ... ] | |
AWS_ACCESS_KEY_ID = 'FOO' # | |
AWS_SECRET_ACCESS_KEY = 'BAR' # | |
AWS_S3_CUSTOM_DOMAIN = '' # ... your cloudfront domain | |
USER_MEDIA_STORAGE_BUCKET = '' # ... the S3 bucket for user uploads | |
DEFAULT_FILE_STORAGE = 'aws_storages.UserMediaS3Storage' | |
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' | |
# [ ... snip ... ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment