Skip to content

Instantly share code, notes, and snippets.

@iMerica
Created April 24, 2018 17:56
Show Gist options
  • Save iMerica/051cfd1f21284d5eeab49d27a3f9faeb to your computer and use it in GitHub Desktop.
Save iMerica/051cfd1f21284d5eeab49d27a3f9faeb to your computer and use it in GitHub Desktop.
Django Static Asset Versioning and Auto-Upload to S3/Cloudfront
from storages.backends.s3boto3 import S3Boto3Storage
from django.contrib.staticfiles.storage import ManifestFilesMixin
from tempfile import SpooledTemporaryFile
import os
class CustomStorage(ManifestFilesMixin, S3Boto3Storage):
""" Handles File Versioning + File Uploading to our CDN """
def _save_content(self, obj, content, parameters):
""" Hack to work around I/O error in Boto """
content.seek(0, os.SEEK_SET)
content_autoclose = SpooledTemporaryFile()
content_autoclose.write(content.read())
super(CustomStorage, self)._save_content(obj, content_autoclose, parameters)
if not content_autoclose.closed:
content_autoclose.close()
@iMerica
Copy link
Author

iMerica commented Apr 24, 2018

In your Django settings, set:

STATICFILES_STORAGE = 'path.to.storage.CustomStorage'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment