问题描述
我正在使用django存储与s3boto后端。根据这个问题,我有一堆文件(所有文件)都具有内容类型'application / octet-stream'。鉴于我有一个< class'boto.s3.key.Key'>
的实例,如何设置content_type? 在[29]中:a.file.file.key.content_type
Out [29]:'application / octet-stream'
在[30]中:mimetypes.guess_type(a.file.file.key.name)[0]
输出[30]:'image / jpeg'
在[ 31]:type(a.file.file.key)
输出[31]:< class'boto.s3.key.Key'>
没有办法修改内容类型(或任何其他元数据)在文件创建后与文件关联。但是,您可以在服务器端复制文件,并在此过程中修改元数据。这是github的一个要点,应该有助于:
内容:
import boto
s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')
#将密钥复制到本身,保留ACL,但更改内容类型
key.copy(key.bucket,key.name,preserve_acl = True,
metadata = { 'content-type':'text / plain'})
key = bucket.lookup('mykey')
print key.content_type
Mitch
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
这篇关于使用boto,在s3上已经存在的文件上设置content_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!