Using boto, set content_type in files that are already present on s3

I am using django repositories with s3boto backend. According to this problem http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by I have a bunch of files (all of them) that have the content type "application / octet-stream". Given that I have an instance of <class 'boto.s3.key.Key'> , how can I set the content_type?

 In [29]: a.file.file.key.content_type Out[29]: 'application/octet-stream' In [30]: mimetypes.guess_type(a.file.file.key.name)[0] Out[30]: 'image/jpeg' In [31]: type(a.file.file.key) Out[31]: <class 'boto.s3.key.Key'> 
+7
source share
1 answer

It is not possible to change the type of content (or any other metadata) associated with a file after it is created. However, you can copy the file on the server side and change the metadata in this process. Here is the github gist that should help:

https://gist.github.com/1791086

Content:

 import boto s3 = boto.connect_s3() bucket = s3.lookup('mybucket') key = bucket.lookup('mykey') # Copy the key onto itself, preserving the ACL but changing the content-type key.copy(key.bucket, key.name, preserve_acl=True, metadata={'Content-Type': 'text/plain'}) key = bucket.lookup('mykey') print key.content_type 

Mitch

+16
source

All Articles