Используя boto, установите content_type для файлов, которые уже присутствуют на s3.

Я использую хранилища django с бэкэндом s3boto. В соответствии с этой проблемой http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by У меня есть куча файлов (все они) с типом контента «application/octet-stream». Учитывая, что у меня есть экземпляр <class 'boto.s3.key.Key'>, как я могу установить 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'>

person Skylar Saveland    schedule 10.02.2012    source источник


Ответы (1)


Невозможно изменить тип содержимого (или любые другие метаданные), связанные с файлом после его создания. Однако вы можете скопировать файл на стороне сервера и изменить метаданные в процессе. Вот суть на github, которая должна помочь:

https://gist.github.com/1791086

Содержание:

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

Митч

person garnaat    schedule 10.02.2012