通过 boto 3 上传 AWS 静态站点文件设置正确的内容类型

AWS static site file upload via boto 3 set the right content-types

托管在 AWS 上的静态站点的不同类型文件的正确内容类型是什么如何通过 boto3 以智能方式设置这些?

我使用 upload_file method:

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 实现?

非常感谢您的帮助!

有一个 python 内置库来猜测 mimetypes。

所以你可以先查找每个文件名。它是这样工作的:

import mimetypes
print(mimetypes.guess_type('filename.html'))

结果:

('text/html', None)

在你的代码中。我还稍微改进了代码相对于 windows 路径的可移植性。现在它会做同样的事情,但可以通过查找将在任何路径中使用的特定于平台的分隔符 (os.path.sep) 移植到 Unix 平台。

import boto3
import mimetypes

s3 = boto3.resource('s3')
bucket = s3.Bucket('allecijfers.nl')

for root, dirs, files in os.walk(local_root + local_dir):
    for filename in files:
        # construct the full local path (Not sure why you were converting to a
        # unix path when you'd want this correctly as a windows path
        local_path = os.path.join(root, filename)

        # construct the full S3 path
        relative_path = os.path.relpath(local_path, local_root)
        s3_path = relative_path.replace(os.path.sep,"/")
        # Get content type guess
        content_type = mimetypes.guess_type(filename)[0]
        bucket.upload_file(
            local_path,
            s3_path,
            ExtraArgs={'ACL': 'public-read', 'ContentType': content_type}
        )