Skip to content

Instantly share code, notes, and snippets.

@idan
Created November 3, 2012 20:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idan/4008567 to your computer and use it in GitHub Desktop.
Save idan/4008567 to your computer and use it in GitHub Desktop.
Protocol-Relative S3BotoStorage for django-storages
from urlparse import urlsplit, urlunsplit
from storages.backends.s3boto import S3BotoStorage
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)
@bmispelon
Copy link

The result of urlsplit is a namedtuple, so you can use its _replace method.

It also has a geturl method which saves you from using urlunsplit:

return urlsplit(url)._replace(scheme='').geturl()

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