Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Allow configuring static/media to point to S3
Browse files Browse the repository at this point in the history
At time of this commit there are outstanding PRs that will fix the
usage of iam roles instead of just regular accounts. See
jschneier/django-storages#282. Instead of
forking the library with those changes I made a work-around by setting
the problematic logic to False. Its a misleading addition since those
attributes actually do get sucked in via env vars.
  • Loading branch information
msheiny committed Jul 10, 2018
1 parent 48a5166 commit ca0bd33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 14 additions & 0 deletions securethenews/s3_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage


class StaticStorage(S3Boto3Storage):
location = settings.AWS_S3_STATIC_PATH
access_key = False
secret_key = False


class MediaStorage(S3Boto3Storage):
location = settings.AWS_S3_MEDIA_PATH
access_key = False
secret_key = False
23 changes: 21 additions & 2 deletions securethenews/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
}
}

STATIC_ROOT = os.environ['DJANGO_STATIC_ROOT']
MEDIA_ROOT = os.environ['DJANGO_MEDIA_ROOT']

try:
es_host = os.environ.get('DJANGO_ES_HOST', 'disable')
Expand Down Expand Up @@ -82,3 +80,24 @@
if os.environ.get('PIWIK_DOMAIN_PATH'):
PIWIK_DOMAIN_PATH = os.environ.get('PIWIK_DOMAIN_PATH')
PIWIK_SITE_ID = os.environ.get('PIWIK_SITE_ID', '1')


if os.environ.get('AWS_SESSION_TOKEN'):
INSTALLED_APPS.append('storages') # noqa: F405

AWS_S3_MEDIA_PATH = os.environ.get('AWS_S3_MEDIA_PATH', 'media')
AWS_S3_STATIC_PATH = os.environ.get('AWS_S3_STATIC_PATH', 'static')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME = os.environ.get('AWS_S3_REGION_NAME', 'us-east-1')

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_SESSION_TOKEN = os.environ.get('AWS_SESSION_TOKEN', '')
AWS_S3_USE_SSL = True
AWS_S3_SIGNATURE_VERSION = os.environ.get("AWS_S3_SIG_VER", "s3v4")
STATICFILES_STORAGE = "securethenews.s3_storage.StaticStorage"
DEFAULT_FILE_STORAGE = "securethenews.s3_storage.MediaStorage"

else:
STATIC_ROOT = os.environ['DJANGO_STATIC_ROOT']
MEDIA_ROOT = os.environ['DJANGO_MEDIA_ROOT']

0 comments on commit ca0bd33

Please sign in to comment.