问题描述
我使用Django储藏与s3boto后端。根据这个问题,<一个href="http://$c$c.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by">http://$c$c.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by我有一帮有内容类型应用程序/八位字节流文件(全部)的。由于我有一个实例&LT;类的boto.s3.key.Key'&GT;
,我怎么可以设置CONTENT_TYPE
在[29]:a.file.file.key.content_type
出[29]:应用程序/八位字节流
在[30]:mimetypes.guess_type(a.file.file.key.name)[0]
出[30]:为image / jpeg
在[31]:式(a.file.file.key)
出[31]:其中,类'boto.s3.key.Key'&GT;
有没有办法来修改一个文件,它已被创建之后相关联的内容类型(或任何其他元数据)。你可以,但是,复制在服务器端的文件和修改过程中的元数据。这里有一个要点在GitHub上,应该帮助:
https://gist.github.com/1791086
内容:
进口博托
S3 = boto.connect_s3()
斗= s3.lookup('mybucket)
键= bucket.lookup('的myKey)
#键复制到自身,preserving ACL,但改变内容类型
key.copy(key.bucket,key.name,preserve_acl = TRUE,
元数据= {'的Content-Type:text / plain的'})
键= bucket.lookup('的myKey)
打印key.content_type
米奇
I'm using django storages with the s3boto backend. As per this issue, 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 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'>
There is no way to modify the content type (or any other metadata) associated with a file after it has been created. You can, however, copy the file on the server side and modify the metadata in the process. Here is a gist on github that should help:
https://gist.github.com/1791086
Contents:
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
这篇关于其上已经使用的文件宝途,设置CONTENT_TYPE $在S3 p $ psent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!