SaltyCrane Blog — Notes on JavaScript and web development

Setting the Expires header for S3 media using Python and boto

Install boto

$ pip install boto 
$ pip freeze |grep boto 
boto==2.2.1 

Script

This script sets the "Expires" header 25 years from the current date for all the files starting with the prefix "mydirectory". Replace the access key id, secret access key, and bucket.

import mimetypes
from datetime import datetime, timedelta

from boto.s3.connection import S3Connection


AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
BUCKET_NAME = 'mybucket'
PREFIX = 'mydirectory'


def main():
    conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(BUCKET_NAME)
    key_list = bucket.get_all_keys(prefix=PREFIX)
    for key in key_list:
        content_type, unused = mimetypes.guess_type(key.name)
        if not content_type:
            content_type = 'text/plain'
        expires = datetime.utcnow() + timedelta(days=(25 * 365))
        expires = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
        metadata = {'Expires': expires, 'Content-Type': content_type}
        print key.name, metadata
        key.copy(BUCKET_NAME, key, metadata=metadata, preserve_acl=True)


if __name__ == '__main__':
    main()

References

Comments


#1 Jean commented on :

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21

To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.