【发布时间】:2018-10-09 04:26:39
【问题描述】:
对于托管在 AWS 上的静态站点的不同类型的文件,正确的内容类型是什么以及如何通过 boto3 以智能方式设置这些内容?
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('allecijfers.nl')
bucket.upload_file('C:/Hugo/Sites/allecijfers/public/test/index.html', 'test/index.html', ExtraArgs={'ACL': 'public-read', 'ContentType': 'text/html'})
这适用于 html 文件。我最初忽略了导致文件下载的 ExtraArgs(可能是因为内容类型是二进制的?)。我发现this page 声明了几种内容类型,但我不确定如何应用它。
例如可能 CSS 文件应该使用“ContentType”上传:“text/css”。 但是js文件,index.xml等呢?以及如何以聪明的方式做到这一点?仅供参考,这是我当前从 Windows 上传到 AWS 的脚本,这需要 string.replace("\","/") 这可能也不是最聪明的?
for root, dirs, files in os.walk(local_root + local_dir):
for filename in files:
# construct the full local path
local_path = os.path.join(root, filename).replace("\\","/")
# construct the full S3 path
relative_path = os.path.relpath(local_path, local_root)
s3_path = os.path.join(relative_path).replace("\\","/")
bucket.upload_file(local_path, s3_path, ExtraArgs={'ACL': 'public-read', 'ContentType': 'text/html'})
我使用 AWS CLI 从同一来源将完整的 Hugo 站点上传到同一 S3 存储桶,并且无需指定内容类型即可完美运行,这是否也可以通过 boto 3 实现?
非常感谢您的帮助!
【问题讨论】:
标签: amazon-web-services amazon-s3 boto3 hugo