From 1cbaa942d0e386c544dab495d02dccfb2813eb91 Mon Sep 17 00:00:00 2001 From: Joe Friedl Date: Mon, 10 Feb 2014 13:22:54 -0500 Subject: [PATCH 1/4] Add docs for the S3_URL_STYLE setting --- docs/index.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 2ba46b5..5226250 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -184,6 +184,12 @@ 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` Specifies the style for S3 URLs. 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` From c2c002ff2dc80c75eb658ed1bee06d8ca1952361 Mon Sep 17 00:00:00 2001 From: Joe Friedl Date: Mon, 10 Feb 2014 13:29:57 -0500 Subject: [PATCH 2/4] Add a default for S3_URL_STYLE --- flask_s3.py | 3 ++- tests/test_flask_static.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index 20637bc..fec208b 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -220,7 +220,8 @@ class FlaskS3(object): ('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) diff --git a/tests/test_flask_static.py b/tests/test_flask_static.py index b1831db..d4ba9f1 100644 --- a/tests/test_flask_static.py +++ b/tests/test_flask_static.py @@ -30,7 +30,8 @@ class FlaskStaticTest(unittest.TestCase): 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) From 2883db1e3db7b79ae2b4e7639ef7380dff3f9a06 Mon Sep 17 00:00:00 2001 From: Joe Friedl Date: Mon, 10 Feb 2014 13:45:49 -0500 Subject: [PATCH 3/4] Make S3 URL style configurable --- flask_s3.py | 16 ++++++++++++++-- tests/test_flask_static.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index fec208b..5d9957c 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -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) diff --git a/tests/test_flask_static.py b/tests/test_flask_static.py index d4ba9f1..d404229 100644 --- a/tests/test_flask_static.py +++ b/tests/test_flask_static.py @@ -122,6 +122,18 @@ class UrlTests(unittest.TestCase): 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): From 4a73903306ccb2bc3bcdf078097626893ed877eb Mon Sep 17 00:00:00 2001 From: Joe Friedl Date: Mon, 10 Feb 2014 13:48:55 -0500 Subject: [PATCH 4/4] Make docs for S3_URL_STYLE less redundant --- docs/index.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 5226250..5ae1cc3 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -184,10 +184,9 @@ 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` Specifies the style for S3 URLs. 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_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