Issue #105 resolved

Add option to produce protocol-relative URLs with S3BotoStorage

idangazit avataridangazit created an issue

Currently, S3BotoStorage returns either HTTP or HTTPS URLs from the url() method [1], depending on the value of secure_urls.

If you're using S3Boto and want to return protocol-relative URLs [2], there's currently no way to ask for this behavior. Protocol-relative URL's omit the protocol and use whatever the browser is currently using so as to avoid mixed-content warnings.

This sort of thing would be especially useful in conjunction with something like django-staticfiles, where I'd like my site's media to work regardless of whether the browser is accessing via HTTP or HTTPS.

My ghetto subclass hack[3] to force this is ugly. More settings are always suspect, but in this case I think they make sense.

Two possible approaches:

Approach 1 - More Backwards Compatible

Add another setting, AWS_S3_PROTOCOL_RELATIVE_URLS. If it's true, short-circuit the existing AWS_S3_SECURE_URLS setting and always return protocol-relative URLs.

Approach 2: Cleaner

Get rid of AWS_S3_SECURE_URLS and instead use something like AWS_S3_URL_PROTOCOL. It can be a string like 'http' or 'https'. A blank string would result in a protocol-relative URL.

Side benefit: this would be future-compatible with SPDY :)

  1. http://code.welldev.org/django-storages/src/a59c9ea953ac/storages/backends/s3boto.py#cl-256
  2. http://paulirish.com/2010/the-protocol-relative-url/
  3. https://gist.github.com/1638695

Comments (4)

  1. pendletongp

    I think this is a good idea. +1

    I think Approach #2 is the right way to go. It can be made backwards compatible by setting the default AWS_S3_URL_PROTOCOL based off of AWS_S3_SECURE_URLS and raise a depreciation warning if AWS_S3_SECURE_URLS is used.

    This is a simple untested example of concept:

    import warnings
    AWS_S3_URL_PROTOCOL = getattr(settings, 'AWS_S3_URL_PROTOCOL', None)
    if AWS_S3_URL_PROTOCOL is None:
    	secure = getattr(settings, 'AWS_S3_SECURE_URLS', None)
    	if secure is None:
    		AWS_S3_URL_PROTOCOL = ""
    	else:
    		warnings.warn("AWS_S3_SECURE_URLS is depreciated.  Use AWS_S3_URL_PROTOCOL instead.", DeprecationWarning)
    		if secure:
    			AWS_S3_URL_PROTOCOL = "https"
    		else:
    			AWS_S3_URL_PROTOCOL = "http"
    

    Then support for AWS_S3_SECURE_URLS can be removed at some point.

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.