使用python boto,如何修改Http Headers?
在我的S3 bucket中,我有一个名为"shop"的文件,由于我上传它时没有文件扩展名,我必须手动设置HttpHeader:ContentType=text/html/
我想使用一个使用boto的python脚本来为所有需要它的文件设置这个头。但是,我找不到一个更改标头的方法。
import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key
ak = " ... key"
sk = " ... key"
bucketname = " ... "
c = S3Connection(ak, sk)
def setcontenttype():
c = S3Connection(ak, sk)
bucket = c.get_bucket(bucketname)
keys = bucket.get_all_keys()
for key in keys:
ext = os.path.splitext(key.name)[1]
if ext == "" and not key.name.endswith("/"):
print key.name
k.set_contents_from_string(k.get_contents_as_string(), {"Content-Type":"text/html"}, True)
s3_conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = s3_conn.get_bucket(s3_bucket_name)
keys = bucket.list()
for key in keys:
key = bucket.get_key(key.name)
metadata = key.metadata
metadata['Content-Type'] = "text/html"
key.copy(s3_bucket_name, key, metadata=metadata, preserve_acl=True)
你必须知道它将取代旧的元数据,所以你应该复制所有其他的元数据头(比如缓存控制…)