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')]