Boto – set content type and other headers upon upload

I always though that you can set the Amason S3’s response headers when using boto’s generate_url API. I was wrong. We can only set those things upon upload, perhaps the docs I am reading is outdated or I’m reading it totally wrong.

Below is the simple code that uses boto library that sets the content type into an image and sets some caching headers.

import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key

conn = S3Connection(access_key, access_secret)
bucket = conn.get_bucket(bucket_name)
img = Key(bucket)
img.key = destination_path
img.set_contents_from_filename(source_file,headers={'Content-Type': 'image/jpeg', 
                                                    'Cache-Control': 'max-age=3600'})

That’s it!

This entry was posted in Python and tagged , , , , . Bookmark the permalink.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *