Skip to content

Instantly share code, notes, and snippets.

@ephes
Created February 21, 2025 16:10
Show Gist options
  • Save ephes/8b91335c679768b314c4eacc368f9cb2 to your computer and use it in GitHub Desktop.
Save ephes/8b91335c679768b314c4eacc368f9cb2 to your computer and use it in GitHub Desktop.
# STORAGE CONFIGURATION
# ------------------------------------------------------------------------------
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
AWS_AUTO_CREATE_BUCKET = True
AWS_QUERYSTRING_AUTH = False
AWS_S3_REGION_NAME = "eu-central-1"
AWS_S3_SIGNATURE_VERSION = "s3v4"
AWS_S3_FILE_OVERWRITE = True
AWS_S3_CUSTOM_DOMAIN = env("CLOUDFRONT_DOMAIN")
# AWS cache settings, don't change unless you know what you're doing:
AWS_EXPIRY = 60 * 60 * 24 * 7
# TODO See: https://github.com/jschneier/django-storages/issues/47
# Revert the following and use str after the above-mentioned bug is fixed in
# either django-storage-redux or boto
control = "max-age=%d, s-maxage=%d, must-revalidate" % (AWS_EXPIRY, AWS_EXPIRY)
AWS_HEADERS = {"Cache-Control": bytes(control, encoding="latin-1")}
from storages.backends.s3boto3 import S3Boto3Storage
class CustomS3Boto3Storage(S3Boto3Storage):
"""
This is our custom version of S3Boto3Storage that fixes a bug in
boto3 where the passed in file is closed upon upload.
https://github.com/boto/boto3/issues/929
https://github.com/matthewwithanm/django-imagekit/issues/391
"""
file_overwrite = False
default_acl = "public-read"
def _save_content(self, obj, content, parameters):
"""
We create a clone of the content file as when this is passed to boto3
it wrongly closes the file upon upload where as the storage backend
expects it to still be open
"""
# Seek our content back to the start
content.seek(0, os.SEEK_SET)
# Create a temporary file that will write to disk after a specified size
content_autoclose = SpooledTemporaryFile()
# Write our original content into our copy that will be closed by boto3
content_autoclose.write(content.read())
# Upload the object which will auto close the content_autoclose instance
super()._save_content(obj, content_autoclose, parameters)
# Cleanup if this is fixed upstream our duplicate should always close
if not content_autoclose.closed:
content_autoclose.close()
STORAGES = {
"default": {"BACKEND": "config.settings.local.CustomS3Boto3Storage"},
"staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
"production": {"BACKEND": "config.settings.local.CustomS3Boto3Storage"},
"backup": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
"OPTIONS": {
"location": ROOT_DIR.path("backups").path("media"),
},
},
}
MEDIA_URL = "https://s3.amazonaws.com/%s/" % AWS_STORAGE_BUCKET_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment