Merge pull request #8 from eriktaubeneck/master

Added support for AWS CloudFront (or any other CDN)
This commit is contained in:
Edward Robinson
2013-06-12 15:12:04 -07:00
4 changed files with 19 additions and 1 deletions
+1
View File
@@ -4,3 +4,4 @@ Contributors
* Edward Robinson (e-dard)
* Rehan Dalal (rehandalal)
* Hannes Ljungberg (hannseman)
* Erik Taubeneck (eriktaubeneck)
+5
View File
@@ -177,6 +177,11 @@ uploading assets to S3.
`S3_BUCKET_DOMAIN` The domain part of the URI for your S3 bucket. You
probably won't need to change this.
**Default:** ``u's3.amazonaws.com'``
'S3_CDN_DOMAIN' AWS makes it easy to attach CloudFront to an S3
bucket. If you want to use this or another CDN,
set the base domain here. This is distinct from the
'S3_BUCKET_DOMAIN` since it will not include the
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_USE_HTTPS` Specifies whether or not to serve your assets
+4
View File
@@ -38,6 +38,8 @@ def url_for(endpoint, **values):
scheme = 'https'
bucket_path = '%s.%s' % (app.config['S3_BUCKET_NAME'],
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)
return urls.build(endpoint, values=values, force_external=True)
return flask_url_for(endpoint, **values)
@@ -219,8 +221,10 @@ class FlaskS3(object):
('USE_S3', True),
('USE_S3_DEBUG', False),
('S3_BUCKET_DOMAIN', 's3.amazonaws.com'),
('S3_CDN_DOMAIN', ''),
('S3_USE_CACHE_CONTROL', False),
('S3_HEADERS', {})]
for k, v in defaults:
app.config.setdefault(k, v)
+9 -1
View File
@@ -29,7 +29,8 @@ class FlaskStaticTest(unittest.TestCase):
""" Tests configuration vars exist. """
FlaskS3(self.app)
defaults = ('S3_USE_HTTPS', 'USE_S3', 'USE_S3_DEBUG',
'S3_BUCKET_DOMAIN', 'S3_USE_CACHE_CONTROL', 'S3_HEADERS')
'S3_BUCKET_DOMAIN', 'S3_CDN_DOMAIN',
'S3_USE_CACHE_CONTROL', 'S3_HEADERS')
for default in defaults:
self.assertIn(default, self.app.config)
@@ -41,6 +42,7 @@ class UrlTests(unittest.TestCase):
self.app.config['S3_BUCKET_NAME'] = 'foo'
self.app.config['S3_USE_HTTPS'] = True
self.app.config['S3_BUCKET_DOMAIN'] = 's3.amazonaws.com'
self.app.config['S3_CDN_DOMAIN'] = ''
@self.app.route('/<url_for_string>')
def a(url_for_string):
@@ -108,6 +110,12 @@ class UrlTests(unittest.TestCase):
exp = 'https://foo.s3.amazonaws.com/admin-static/bah.js'
self.assertEquals(self.client_get(ufs).data, exp)
def test_url_for_cdn_domain(self):
self.app.config['S3_CDN_DOMAIN'] = 'foo.cloudfront.net'
ufs = "{{url_for('static', filename='bah.js')}}"
exp = 'https://foo.cloudfront.net/static/bah.js'
self.assertEquals(self.client_get(ufs).data, exp)
class S3Tests(unittest.TestCase):