Skip to content

Instantly share code, notes, and snippets.

@kolynzb
Created April 13, 2023 08:32
Show Gist options
  • Save kolynzb/4a925cdaf986b2c079fb9ae3af7b558a to your computer and use it in GitHub Desktop.
Save kolynzb/4a925cdaf986b2c079fb9ae3af7b558a to your computer and use it in GitHub Desktop.
AWS S3 storages Configuration with Django
# Configuration for AWS S3; https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
AWS_ACCESS_KEY_ID=<>
AWS_SECRET_ACCESS_KEY=<>
AWS_STORAGE_BUCKET_NAME=<>
AWS_S3_REGION_NAME=<>
boto3==1.26.59
Django==4.1.5
django-storages==1.13.2
gunicorn==20.1.0
INSTALLED_APPS += (
"gunicorn",
"storages",
)
# AWS S3 Configuration
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_DEFAULT_ACL = None
# AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_VERIFY = True
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_S3_FILE_OVERWRITE = False
AWS_S3_REGION_NAME = config('AWS_S3_REGION_NAME')
# s3 static settings
# STATIC_LOCATION = 'static'
# STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
# STATICFILES_STORAGE = 'config.storage_backends.StaticStorage'
# s3 public media settings
# PUBLIC_MEDIA_LOCATION = 'media'
# MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/'
# DEFAULT_FILE_STORAGE = 'config.storage_backends.PublicMediaStorage'
# s3 private media settings
# PRIVATE_MEDIA_LOCATION = 'private'
# PRIVATE_FILE_STORAGE = 'config.storage_backends.PrivateMediaStorage'
# <Djnago project>/storage_backends.py
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
location = 'static'
default_acl = 'public-read'
class PublicMediaStorage(S3Boto3Storage):
location = 'media'
default_acl = 'public-read'
file_overwrite = False
class PrivateMediaStorage(S3Boto3Storage):
location = 'private'
default_acl = 'private'
file_overwrite = False
custom_domain = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment