Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Updating caching headers for Amazon S3 and CloudFront
#!/usr/bin/env python
Updates S3 objects with new cache-control headers.
python <bucket_name> <keys>*
Updates all keys of bucket::
Updates only one key::
python d39c2.gif
Read more here::
:copyright: by Amir Salihefendic ( )
:license: MIT
import sys
import mimetypes
import email
import time
import types
from datetime import datetime, timedelta
from boto.s3.connection import S3Connection
from boto.cloudfront import CloudFrontConnection
#--- AWS credentials ----------------------------------------------
AWS_KEY = '...'
AWS_SECRET = '...'
#--- Main function ----------------------------------------------
def main(s3_bucket_name, keys=None):
s3_conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = s3_conn.get_bucket(s3_bucket_name)
if not keys:
keys = bucket.list()
for key in keys:
if type(key) == types.StringType:
key_name = key
key = bucket.get_key(key)
if not key:
print 'Key not found %s' % key_name
# Force a fetch to get metadata
# see this why:
key = bucket.get_key(
aggressive_headers = _get_aggressive_cache_headers(key)
key.copy(s3_bucket_name, key, metadata=aggressive_headers, preserve_acl=True)
print 'Updated headers for %s' %
#--- Helpers ----------------------------------------------
def _get_aggressive_cache_headers(key):
metadata = key.metadata
metadata['Content-Type'] = key.content_type
# HTTP/1.0 (5 years)
metadata['Expires'] = '%s GMT' %\
time.mktime(( +
# HTTP/1.1 (5 years)
metadata['Cache-Control'] = 'max-age=%d, public' % (3600 * 24 * 360 * 5)
return metadata
if __name__ == '__main__':
main( sys.argv[1],
sys.argv[2:] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.