如何将Python Boto库与S3一起使用,其中生成的URL将成为我对Amazon S3 Server的CNAME子域。
默认情况下,它使用默认格式 BUCKETNAME.s3.amazonaws.com ,但S3支持使用CNAME自定义域别名(因此您可以使用custom.domain.com - > CNAME - >自定义.domain.com.s3.amazonaws.com其中“custom.domain.com”是存储桶。AWS Documentation
我看到boto库有boto.s3.connection.SubdomainCallingFormat和类boto.s3.connection.VHostCallingFormat ......
任何人都知道如何设置boto.s3,其中生成的URL是我自己的自定义域而不是默认值?
答案 0 :(得分:2)
完成后,我编写的以下代码段会将URL打印到密钥中的所有文件中:
import sys
import boto.s3
from boto.s3.connection import VHostCallingFormat
from boto.s3.connection import S3Connection
def main():
access_key = "<AWS_ACCESS_KEY>"
secret_key = "<AWS_SECRET_KEY>"
bucket = "custom.domain.com"
# assuming you have your files organized with keys
key_prefix = "css"
key_prefix = key_prefix + "/"
conn = S3Connection(access_key, secret_key, calling_format=VHostCallingFormat())
bucket = conn.get_bucket(bucket)
# get all the keys with the prefix 'css/' inside said bucket
keys = bucket.get_all_keys(prefix=key_prefix)
for k in keys:
print k.generate_url(3600, query_auth=False, force_http=True)
# output:
# http://custom.domain.com/css/ie.css
# http://custom.domain.com/css/print.css
# http://custom.domain.com/css/screen.css
# http://custom.domain.com/css/style.min.css
if __name__ == '__main__':
main()