【问题标题】:Uploading .html file to S3 Static website hosted bucket causes download of .html file in browser将 .html 文件上传到 S3 静态网站托管存储桶会导致在浏览器中下载 .html 文件
【发布时间】:2018-03-02 00:05:23
【问题描述】:

我有一个启用了“静态网站托管”的 S3 存储桶。如果我通过 AWS 控制台将 html 文件上传到存储桶,则 html 文件会成功打开。如果我使用 AWS CLI 上传文件,文件被下载而不是显示在浏览器中,为什么?

第一个文件在这里:https://s3.amazonaws.com/test-bucket-for-stackoverflow-post/page1.html

第二个文件在这里:https://s3.amazonaws.com/test-bucket-for-stackoverflow-post/page2.html

我在 AWS 控制台中上传了第一个文件,第二个文件是使用以下命令上传的:

     aws s3api put-object --bucket test-bucket-for-stackoverflow-post  --key page2.html --body page2.html

【问题讨论】:

    标签: amazon-s3 aws-cli


    【解决方案1】:

    第二个文件因其“Content-Type”标头而被下载。该标题是:

    Content-Type: binary/octet-stream
    

    如果你想让它显示,它应该是:

    Content-Type: text/html
    

    尝试将--content-type text/html 添加到您的put-object 命令中。

    参考:https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html

    【讨论】:

    • 谢谢,就是这样,我将 s3api 调用更改为 aws s3api put-object --bucket test-bucket-for-stackoverflow-post --key page3.html --body page2.html - -content-type text/html
    • 如果您使用aws s3 cp,它将尝试为您猜测内容类型。它还将尝试尽可能高效地上传文件。
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多