我正在使用 django 存储和 s3boto 后端。根据这个问题,http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by 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'>
创建文件后,无法修改与该文件关联的内容类型(或任何其他元数据)。但是,您可以在服务器端复制文件并在此过程中修改元数据。这是 github 上的一个要点,应该会有所帮助:
https://gist.github.com/1791086 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
Mitch
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)