Package: django-cuddlybuddly-storage-s3
Updated Amazon S3 storage from django-storages. Adds more fixes than I can remember, a metadata cache system and some extra utilities for dealing withMEDIA_URLandHTTPS, CloudFront and for creating signed URLs.InstallationAddcuddlybuddly.storage.s3to yourINSTALLED_APPS.SetDEFAULT_FILE_STORAGEtocuddlybuddly.storage.s3.S3Storage(as a string, don’t import it).SetMEDIA_URLto your bucket URL , e.g.http://yourbucket.s3.amazonaws.com/.Enter your AWS credentials in the settings below.SettingsAWS_ACCESS_KEY_IDYour Amazon Web Services access key, as a string.AWS_SECRET_ACCESS_KEYYour Amazon Web Services secret access key, as a string.AWS_STORAGE_BUCKET_NAMEYour Amazon Web Services storage bucket name, as a string.AWS_HEADERSA list of regular expressions which if matched add the headers to the file being uploaded to S3. The patterns are matched from first to last:# see http://developer.yahoo.com/performance/rules.html#expires
AWS_HEADERS = [
    ('^private/', {
        'x-amz-acl': 'private',
        'Expires': 'Thu, 15 Apr 2000 20:00:00 GMT',
        'Cache-Control': 'private, max-age=0'
    }),
    ('.*', {
        'x-amz-acl': 'public-read',
        'Expires': 'Sat, 30 Oct 2010 20:00:00 GMT',
        'Cache-Control': 'public, max-age=31556926'
    })
]x-amz-aclsets the ACL of the file on S3 and defaults toprivate.Expiresis for old HTTP/1.0 caches and must be a perfectly formatted RFC 1123 date to work properly.django.utils.http.http_datecan help you here.Cache-Controlis HTTP/1.1 and takes precedence if supported.max-ageis the number of seconds into the future the response should be cached for.AWS_CALLING_FORMATOptional and defaults toSUBDOMAIN. The way you’d like to call the Amazon Web Services API, for instance if you need to use the old path method:from cuddlybuddly.storage.s3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.PATHCUDDLYBUDDLY_STORAGE_S3_GZIP_CONTENT_TYPESA list of content types that will be gzipped. Defaults to('text/css', 'application/javascript','application/x-javascript').CUDDLYBUDDLY_STORAGE_S3_SKIP_TESTSSet to a true value to skip the tests as they can be pretty slow.CUDDLYBUDDLY_STORAGE_S3_SYNC_EXCLUDEA list of regular expressions of files and folders to ignore when using the synchronize commands. Defaults to['\.svn$','\.git$','\.hg$','Thumbs\.db$','\.DS_Store$'].CUDDLYBUDDLY_STORAGE_S3_KEY_PAIRA tuple of a key pair ID and the contents of the private key from the security credentials page of your AWS account. This is used for signing private CloudFront URLs. For example:settings.CUDDLYBUDDLY_STORAGE_S3_KEY_PAIR = ('PK12345EXAMPLE',
"""-----BEGIN RSA PRIVATE KEY-----
...key contents...
-----END RSA PRIVATE KEY-----""")HTTPSBecause when you use S3 yourMEDIA_URLmust be absolute (i.e. it starts withhttp) it’s more difficult to have URLs that match how the page was requested. The following things should help with that.cuddlybuddly.storage.s3.middleware.ThreadLocalsThis middleware will ensure that the URLs of files retrieved from the database will have the same protocol as how the page was requested.cuddlybuddly.storage.s3.context_processors.mediaThis context processor returnsMEDIA_URLwith the protocol matching how the page was requested.CacheIncluded is a cache system to store file metadata to speed up accessing file metadata such as size and the last modified time. It is disabled by default.FileSystemCacheThe only included cache system isFileSystemCachethat stores the cache on the local disk. To use it, add the following to your settings file:CUDDLYBUDDLY_STORAGE_S3_CACHE = 'cuddlybuddly.storage.s3.cache.FileSystemCache'
CUDDLYBUDDLY_STORAGE_S3_FILE_CACHE_DIR  = '/location/to/store/cache'Custom CacheTo create your own cache system, inherit fromcuddlybuddly.storage.s3.cache.Cacheand implement the following methods:existsmodified_timesavesizeremoveUtilitiescreate_signed_url(file, expires=60, secure=False, private_cloudfront=False, expires_at=None)Creates a signed URL tofilethat will expire inexpiresseconds. Ifsecureis set toTrueanhttpslink will be returned.Theprivate_cloudfrontargument will use they key pair setup withCUDDLYBUDDLY_STORAGE_S3_KEY_PAIRto create signed URLs for a private CloudFront distribution.Theexpires_atargument will overrideexpiresand expire the URL at a specified UNIX timestamp. It was mostly just added for generating consistent URLs for testing.To import it:from cuddlybuddly.storage.s3.utils import create_signed_urlCloudFrontURLs(default,patterns={},https=None)Use this with the context processor or storage backends to return varyingMEDIA_URLorSTATIC_URLdepending on the path to improve page loading times.To use it add something like the following to your settings file:from cuddlybuddly.storage.s3.utils import CloudFrontURLs
MEDIA_URL = CloudFrontURLs('http://cdn1.example.com/', patterns={
    '^images/': 'http://cdn2.example.com/',
    '^banners/': 'http://cdn3.example.com/',
    '^css/': 'http://cdn4.example.com/'
    }, https='https://example.cloudfront.net/')Thehttpsargument is a URL to bypass CloudFront’s lack of HTTPS CNAME support.s3_media_urlTemplate TagThis is for use withCloudFrontURLsand will return the appropriate URL if a match is found.Usage:{% load s3_tags %}
{% s3_media_url 'css/common.css' %}ForHTTPS, thecuddlybuddly.storage.s3.middleware.ThreadLocalsmiddleware must also be used.s3_static_urlTemplate TagThe same ass3_media_urlbut usesSTATIC_URLinstead.cuddlybuddly.storage.s3.S3StorageStaticStorage BackendA version of the storage backend that usesSTATIC_URLinstead. For use withSTATICFILES_STORAGEand thestatictemplate tag fromcontrib.staticfiles.Commandscb_s3_sync_mediaSynchronizes a directory with your S3 bucket. It will skip files that are already up to date or newer in the bucket but will not remove old files as that has the potential to go very wrong. The headers specified inAWS_HEADERSwill be applied.It has the following options:--cache,-c- Get the modified times of files from the cache (if available) instead of checking S3. This is faster but could be inaccurate.--dir,-d- The directory to synchronize with your bucket, defaults toMEDIA_ROOT.--exclude,-e- A comma separated list of regular expressions to ignore files or folders. Defaults toCUDDLYBUDDLY_STORAGE_S3_SYNC_EXCLUDE.--force,-f- Uploads all files even if the version in the bucket is up to date.--prefix,-p- A prefix to prepend to every file uploaded, i.e. a subfolder to place the files in.cb_s3_sync_staticExactly the same ascb_s3_sync_mediaexcept thatdirdefeaults toSTATIC_ROOT.A note on the testsThe tests intests/s3test.pyare pretty much straight from Amazon but have a tendency to fail if you run them too often / too quickly. When they do this they sometimes leave behind files or buckets in your account that you will need to go and delete to make the tests pass again.The signed URL tests will also fail if your computer’s clock is too far off from Amazon’s servers.