Skip to content

Instantly share code, notes, and snippets.

@tbarbugli
Created July 25, 2012 21:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbarbugli/3178905 to your computer and use it in GitHub Desktop.
Save tbarbugli/3178905 to your computer and use it in GitHub Desktop.
S3BotoStorage
from boto.utils import parse_ts
from django.core.files.storage import get_storage_class
from storages.backends.s3boto import S3BotoStorage
"""
make sure you have this setting
AWS_PRELOAD_METADATA = True
and that you have python-dateutils==1.5 installed
"""
class CachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that saves the files locally, too.
"""
def __init__(self, *args, **kwargs):
super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def save(self, name, content):
name = super(CachedS3BotoStorage, self).save(name, content)
self.local_storage._save(name, content)
return name
def modified_time(self, name):
name = self._normalize_name(self._clean_name(name))
entry = self.entries.get(name)
if entry is None:
entry = self.bucket.get_key(self._encode_name(name))
# Parse the last_modified string to a local datetime object.
return parse_ts(entry.last_modified)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment