Skip to content

Instantly share code, notes, and snippets.

@idan
Created January 19, 2012 08:11
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save idan/1638695 to your computer and use it in GitHub Desktop.
Django-staticfiles with storage on S3, delivering Protocol-relative URLs
from urlparse import urlsplit, urlunsplit
from storages.backends.s3boto import S3BotoStorage
from staticfiles.storage import CachedFilesMixin
class ProtocolRelativeS3BotoStorage(S3BotoStorage):
"""Extends S3BotoStorage to return protocol-relative URLs
See: http://paulirish.com/2010/the-protocol-relative-url/
"""
def url(self, name):
"""Modifies return URLs to be protocol-relative."""
url = super(ProtocolRelativeS3BotoStorage, self).url(name)
parts = list(urlsplit(url))
parts[0] = ''
return urlunsplit(parts)
class S3HashedFilesStorage(CachedFilesMixin, ProtocolRelativeS3BotoStorage):
"""
Extends S3BotoStorage to also save hashed copies (i.e.
with filenames containing the file's MD5 hash) of the
files it saves.
"""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment