使用python boto,如何修改Http Headers?
在我的S3存储桶中,我有一个名为“shop”的文件,因为我上传没有文件扩展名,我必须手动设置Http Header:ContentType = text / html
我想使用boto为所有需要此文件的文件设置此标头。但是,我找不到更改标题的方法。
答案 0 :(得分:6)
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)
答案 1 :(得分:2)
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)
您必须知道它将替换旧的元数据,因此您应该复制所有其他元标题(如缓存控制....)