Skip to content

Instantly share code, notes, and snippets.

@mrcoles
Created Apr 23, 2015
Embed
What would you like to do?
StaticCachedS3BotoStorage - that reads its manifest.json locally
from django.conf import settings
from django.core.files.storage import get_storage_class
from compressor.cache import get_offline_manifest_filename
from storages.backends.s3boto import S3BotoStorage
class StaticCachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that also saves files locally for django-compressor
and also reads the manifest file from the local file-system instead of
S3, so packaged deploys also point to their own manifest.
"""
bucket_name = settings.AWS_BUCKET_NAME_STATIC
# reduced_redundancy = True
# headers = {
# 'Cache-Control': 'max-age=2592000',
# }
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.local_storage = get_storage_class(
'compressor.storage.CompressorFileStorage'
)()
self.offline_manifest_filename = get_offline_manifest_filename()
def save(self, name, content):
name = super().save(name, content)
self.local_storage._save(name, content)
return name
def _is_manifest(self, name):
return self._normalize_name(self._clean_name(name)) == self.offline_manifest_filename
def exists(self, name):
if self._is_manifest(name):
return self.local_storage.exists(name)
else:
return super().exists(name)
def _open(self, name, mode='rb'):
if self._is_manifest(name):
return self.local_storage._open(name, mode)
else:
return super()._open(name, mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment