From 93879c1bff939a60daf41f4ca90312ca12e5c5ce Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Tue, 31 Mar 2015 14:02:52 +1100 Subject: [PATCH 1/3] new S3_FILEPATH_HEADERS setting, to set custom headers only if the filepath matches certain configured regular expressions. --- docs/index.rst | 9 ++++++++- flask_s3.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index 25092fc..ae67aac 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -214,9 +214,16 @@ uploading assets to S3. specified by `flask.url_for`. `S3_HEADERS` Sets custom headers to be sent with each file to S3. **Default:** `{}` +`S3_FILEPATH_HEADERS` Sets custom headers for files whose filepath matches + certain regular expressions. E.g. to add CORS + headers for font files, set this to: + `{r'\.(ttf|woff|woff2|svg|eot)$':` + ` {'Access-Control-Allow-Origin': '*'}` + `}` + **Default:** `{}` `S3_ONLY_MODIFIED` Only upload files that have been modified since last upload to S3. SHA-1 file hashes are used to compute - file changes. You can delete `.file-hashes` from + file changes. You can delete `.file-hashes` from your S3 bucket to force all files to upload again. `S3_CACHE_CONTROL` **Deprecated**. Please use `S3_HEADERS` instead. `S3_USE_CACHE_CONTROL` **Deprecated**. Please use `S3_HEADERS` instead. diff --git a/flask_s3.py b/flask_s3.py index b83fffe..c54a156 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -146,9 +146,20 @@ def _write_files(app, static_url_loc, static_folder, files, bucket, logger.debug("%s excluded from upload" % key_name) else: k = Key(bucket=bucket, name=key_name) + # Set custom headers for header, value in app.config['S3_HEADERS'].iteritems(): k.set_metadata(header, value) + + # Set more custom headers if the filepath matches certain + # configured regular expressions. + filepath_headers = app.config['S3_FILEPATH_HEADERS'] + if filepath_headers: + for filepath_regex, headers in filepath_headers.iteritems(): + if re.search(filepath_regex, file_path): + for header, value in headers.iteritems(): + k.set_metadata(header, value) + k.set_contents_from_filename(file_path) k.make_public() @@ -298,6 +309,7 @@ class FlaskS3(object): ('S3_CDN_DOMAIN', ''), ('S3_USE_CACHE_CONTROL', False), ('S3_HEADERS', {}), + ('S3_FILEPATH_HEADERS', {}), ('S3_ONLY_MODIFIED', False), ('S3_URL_STYLE', 'host')] From 72c05e72aaf086a090852bc10174f4e23dd38c61 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Tue, 31 Mar 2015 14:32:57 +1100 Subject: [PATCH 2/3] update docs to advise that S3_FILEPATH_HEADERS can't be used for CORS headers. --- docs/index.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index ae67aac..5ed9621 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -215,10 +215,12 @@ uploading assets to S3. `S3_HEADERS` Sets custom headers to be sent with each file to S3. **Default:** `{}` `S3_FILEPATH_HEADERS` Sets custom headers for files whose filepath matches - certain regular expressions. E.g. to add CORS - headers for font files, set this to: - `{r'\.(ttf|woff|woff2|svg|eot)$':` - ` {'Access-Control-Allow-Origin': '*'}` + certain regular expressions. (Note that this cannot + be used for CORS, that must be set per S3 bucket + using an XML config string.) E.g. to add custom + metadata when serving text files, set this to: + `{r'\.txt$':` + ` {'Texted-Up-By': 'Mister Foo'}` `}` **Default:** `{}` `S3_ONLY_MODIFIED` Only upload files that have been modified since last From ae2f3fbb493fdaad56d6e2b1b7a526482f917fd5 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Wed, 8 Apr 2015 14:12:23 +1000 Subject: [PATCH 3/3] fix keyerror if S3_HEADERS isn't set in config --- flask_s3.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index c54a156..e116605 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -148,12 +148,14 @@ def _write_files(app, static_url_loc, static_folder, files, bucket, k = Key(bucket=bucket, name=key_name) # Set custom headers - for header, value in app.config['S3_HEADERS'].iteritems(): - k.set_metadata(header, value) + headers = app.config.get('S3_HEADERS') + if headers: + for header, value in headers.iteritems(): + k.set_metadata(header, value) # Set more custom headers if the filepath matches certain # configured regular expressions. - filepath_headers = app.config['S3_FILEPATH_HEADERS'] + filepath_headers = app.config.get('S3_FILEPATH_HEADERS') if filepath_headers: for filepath_regex, headers in filepath_headers.iteritems(): if re.search(filepath_regex, file_path):