Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to use path-style S3 URLs. #21

Merged
merged 4 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ uploading assets to S3.
bucket name in the base url.
`S3_BUCKET_NAME` The desired name for your Amazon S3 bucket. Note:
the name will be visible in all your assets' URLs.
`S3_URL_STYLE` Set to `'host'` to use virtual-host-style URLs,
e.g. ``bucketname.s3.amazonaws.com``. Set to
`'path'` to use path-style URLs, e.g.
``s3.amazonaws.com/bucketname``.
**Default:** `'host'`
`S3_USE_HTTPS` Specifies whether or not to serve your assets
stored in S3 over HTTPS.
**Default:** `True`
Expand Down
19 changes: 16 additions & 3 deletions flask_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ def url_for(endpoint, **values):
scheme = 'http'
if app.config['S3_USE_HTTPS']:
scheme = 'https'
bucket_path = '%s.%s' % (app.config['S3_BUCKET_NAME'],
app.config['S3_BUCKET_DOMAIN'])

if app.config['S3_URL_STYLE'] == 'host':
url_format = '%(bucket_name)s.%(bucket_domain)s'
elif app.config['S3_URL_STYLE'] == 'path':
url_format = '%(bucket_domain)s/%(bucket_name)s'
else:
raise ValueError('Invalid S3 URL style: "%s"'
% app.config['S3_URL_STYLE'])

bucket_path = url_format % {
'bucket_name': app.config['S3_BUCKET_NAME'],
'bucket_domain': app.config['S3_BUCKET_DOMAIN'],
}

if app.config['S3_CDN_DOMAIN']:
bucket_path = '%s' % app.config['S3_CDN_DOMAIN']
urls = app.url_map.bind(bucket_path, url_scheme=scheme)
Expand Down Expand Up @@ -220,7 +232,8 @@ def init_app(self, app):
('S3_BUCKET_DOMAIN', 's3.amazonaws.com'),
('S3_CDN_DOMAIN', ''),
('S3_USE_CACHE_CONTROL', False),
('S3_HEADERS', {})]
('S3_HEADERS', {}),
('S3_URL_STYLE', 'host')]

for k, v in defaults:
app.config.setdefault(k, v)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_flask_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def test_config(self):
FlaskS3(self.app)
defaults = ('S3_USE_HTTPS', 'USE_S3', 'USE_S3_DEBUG',
'S3_BUCKET_DOMAIN', 'S3_CDN_DOMAIN',
'S3_USE_CACHE_CONTROL', 'S3_HEADERS')
'S3_USE_CACHE_CONTROL', 'S3_HEADERS',
'S3_URL_STYLE')
for default in defaults:
self.assertIn(default, self.app.config)

Expand Down Expand Up @@ -121,6 +122,18 @@ def test_url_for_cdn_domain(self):
exp = 'https://foo.cloudfront.net/static/bah.js'
self.assertEquals(self.client_get(ufs).data, exp)

def test_url_for_url_style_path(self):
"""Tests that the URL returned uses the path style."""
self.app.config['S3_URL_STYLE'] = 'path'
ufs = "{{url_for('static', filename='bah.js')}}"
exp = 'https://s3.amazonaws.com/foo/static/bah.js'
self.assertEquals(self.client_get(ufs).data, exp)

def test_url_for_url_style_invalid(self):
"""Tests that an exception is raised for invalid URL styles."""
self.app.config['S3_URL_STYLE'] = 'balderdash'
ufs = "{{url_for('static', filename='bah.js')}}"
self.assertRaises(ValueError, self.client_get, ufs)


class S3Tests(unittest.TestCase):
Expand Down