A Python package for setting HTTP headers on Amazon Web Services (AWS) S3 objects.
Project description
s3headerize
A Python package for setting HTTP headers on Amazon Web Services (AWS) S3 objects.
Header values are configured per-name extension, with an optional catch-all if the object does not match any rules.
Prerequisites
Python 3.x is required.
Limitations
Only Cache-Control and Content-Type headers are currently supported.
Configuring the headers and their values
The headers to set are configured in a YAML file with a list of rules.
For example:
- header: Cache-Control
when:
- extension: .html
then: max-age=300, public
- extension: .css
then: max-age=604800, public
else: max-age=31536000, public
The result of this will be:
- Every
.htmlobject will receive aCache-Controlheader with valuemax-age=300, public. - Every
.cssobject will receive aCache-Controlheader with valuemax-age=604800, public. - All other objects will receive a
Cache-Controlheader with valuemax-age=31536000, public.
The else statement is optional. For example:
- header: Content-Type
when:
- extension: .woff2
then: font/woff2
The result of this will be:
- Every
.woff2object will receive aContent-Typeheader with valuefont/woff2. - The
Content-Typeheader (or lackthereof) on all other objects will be unchanged.
There's a sample file at sample-header-rules.yaml.
Usage
Installation
pip install s3headerize
Command-line
python -m s3headerize --bucket <bucket to update>
--header-rules <path to rules file>
[--dry-run]
[--key-prefix <optional key prefix>]
[--log-level <optional log level>]
For example:
python -m s3headerize --header-rules ./headers-rules.yaml --bucket my-website
In code
To run against a bucket:
from s3headerize import BucketHeaderizer
rules = [
{
'header': 'Cache-Control',
'when': [
{
'extension': '.html',
'then': 'max-age=300, public'
}
],
'else': 'max-age=31536000, public'
},
{
'header': 'Content-Type',
'when': [
{
'extension': '.woff2',
'then': 'font/woff2'
}
]
}
]
bucket_headerizer = BucketHeaderizer(header_rules=rules)
bucket_headerizer.update(bucket='my-website')
To run against specific keys:
from s3headerize import ObjectHeaderizer
rules = [
{
'header': 'Cache-Control',
'when': [
{
'extension': '.html',
'then': 'max-age=300, public'
}
],
'else': 'max-age=31536000, public'
},
{
'header': 'Content-Type',
'when': [
{
'extension': '.woff2',
'then': 'font/woff2'
}
]
}
]
object_headerizer = ObjectHeaderizer(bucket='my-website',
header_rules=rules,
key='index.html')
object_headerizer.update()
Development
Installing dependencies
pip install -e .[dev]
Running tests
python test.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file s3headerize-1.0.tar.gz.
File metadata
- Download URL: s3headerize-1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.7.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8f000c2663d9052456d8d1d9ecd2e5fba0624e2a718f04c8ccf1582b4f09cef
|
|
| MD5 |
e5ff1fd8b82ab821ff95e62b93f751e0
|
|
| BLAKE2b-256 |
60a4f9d3d0a4fc1ebe8916f5e89e93f177570a61fd3e2f47c7fd59351f2032b0
|
File details
Details for the file s3headerize-1.0-py3-none-any.whl.
File metadata
- Download URL: s3headerize-1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.7.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c7e562e14919fd091b1d6d393b2bc8860061362852d6341757b3ed8ef10294d
|
|
| MD5 |
3d922f2f8776da338d0b44ed9ea3e513
|
|
| BLAKE2b-256 |
cc4621fb690bfa9b91548e314b423bc138eae0eb67c836fcd0ee33fb800d2df1
|