Setting cache-cotrol header for Amazon S3 key using boto

S3 does not set a default cache-control header, which means files you upload won’t be cached in the browser by default.

It is amazingly obscure how to do something as simple as setting the cache-control key in Amazon S3. The boto document seems to imply you could do this via key.set_metadata, but no, that won’t work because cache-control is not part of the metadata collection in the boto api. Instead, it’s part of the key attribute: http://boto.readthedocs.org/en/latest/ref/s3.html

I wrote a program to change the cache-control setting for all the objects in a s3 bucket.

First, you need to install boto.

pip install boto

Next, get the script from github. Modified the script to add in your AWS credential and bucket name, then run it:

python set_s3_cache_header.py

Good luck!

Following is some code snippet from the script:

 
from boto.s3.connection import S3Connection
s3_conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = s3_conn.get_bucket(AWS_BUCKET_NAME)
bucket.make_public()
for key in bucket.list():
key = bucket.get_key(key.name)
key.cache_control = 'max-age=%d, public' % (3600 * 24 * 360 * 2)
print key.name + ' ' +  key.cache_control