Skip to content

Commit

Permalink
set s3 storage to manage media and static files in production
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraPoturaeva committed Nov 24, 2023
1 parent 5800195 commit 5c63b9b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ README.md
venv/
.idea/
.pytest_cache/
media/
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ RUN apk add --no-cache --update musl-locales

COPY . /app

ENTRYPOINT ["python"]
CMD ["python", "manage.py", "collectstatic", "--noinput"]

CMD ["manage.py", "runserver", "0.0.0.0:8000"]
CMD ["gunicorn", "sports_events.wsgi:application", "--bind", "0.0.0.0:8000"]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
add-trailing-comma==3.1.0
boto3==1.29.6
dj-database-url==2.1.0
Django==4.2.6
django-extensions==3.2.3
django-storages==1.14.2
flake8==6.1.0
gunicorn==21.2.0
Pillow==10.1.0
Expand Down
35 changes: 24 additions & 11 deletions sports_events/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dj_database_url

ENVIRONMENT = os.getenv('ENV')

if ENVIRONMENT == 'prod':
load_dotenv('.env.prod')
else:
Expand All @@ -31,7 +32,7 @@
SECRET_KEY = os.getenv('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv('DEBUG')
DEBUG = int(os.getenv('DEBUG'))

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(' ')
Expand All @@ -48,6 +49,7 @@
'django.contrib.staticfiles',
'social_django',
'django_extensions',
'storages',

'core',
'users',
Expand Down Expand Up @@ -129,14 +131,29 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
YANDEX_BUCKET_NAME = os.getenv('YANDEX_BUCKET_NAME')

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
if not DEBUG:
STORAGES = {
"default": {
"BACKEND": "yandex_s3_storage.MediaStorage",
},
"staticfiles": {
"BACKEND": "yandex_s3_storage.MediaStorage",
},
}
STATIC_URL = f'https://{os.getenv("AWS_S3_ENDPOINT_URL")}/static/'
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_S3_ENDPOINT_URL = os.getenv('AWS_S3_ENDPOINT_URL')
AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME')
else:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
Expand Down Expand Up @@ -169,7 +186,3 @@
SOCIAL_AUTH_GITHUB_KEY = os.getenv('SOCIAL_AUTH_GITHUB_KEY')
SOCIAL_AUTH_GITHUB_SECRET = os.getenv('SOCIAL_AUTH_GITHUB_SECRET')
SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']

# User-uploaded files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
7 changes: 7 additions & 0 deletions yandex_s3_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from storages.backends.s3boto3 import S3Boto3Storage
from sports_events.settings import YANDEX_BUCKET_NAME


class MediaStorage(S3Boto3Storage):
bucket_name = YANDEX_BUCKET_NAME
file_overwrite = False

0 comments on commit 5c63b9b

Please sign in to comment.