使用 AWS CLI 上传到 S3 时如何设置 Content-Type? [英] How do I set Content-Type when uploading to S3 with AWS CLI?

查看:43
本文介绍了使用 AWS CLI 上传到 S3 时如何设置 Content-Type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 S3 静态网站托管,但它似乎在我的任何未在 AWS 控制台中设置 Content-Type 元数据字段的对象上返回 403.我不知道如何使用 AWS CLI 工具执行此操作.

I'm trying to set up S3 static website hosting, but it appears to return 403 on any of my objects that don't have the Content-Type metadata field set in the AWS console. I cannot figure out how to do this with the AWS CLI tool.

使用 --metadata 选项似乎有效:

Using the --metadata option appears to work:

$ aws s3api put-object --bucket <bucket> --key foo.html --body foo.html --metadata Content-Type=text/html
{
    "ETag": ""fd5ff7743e5ed1e1c304eb1c34e8e39f""
}
$ aws s3api head-object --bucket <bucket> --key foo.html
{
    "AcceptRanges": "bytes",
    "ContentType": "binary/octet-stream",
    "LastModified": "Wed, 15 Apr 2015 06:39:48 GMT",
    "ContentLength": 189,
    "ETag": ""fd5ff7743e5ed1e1c304eb1c34e8e39f"",
    "Metadata": {
        "content-type": "text/html"
    }
}

但是对象上的 Content-Type 字段在 AWS 控制台的元数据"部分中不可见,并且我在尝试在浏览器中访问文件时收到 403.

But the Content-Type field on the object isn't visible in the "Metadata" section of the AWS console, and I get a 403 when trying to access the file in a browser.

使用 --content-type 选项也不起作用:

Using the --content-type option also doesn't work:

$ aws s3api put-object --bucket <bucket> --key foo.html --body foo.html --content-type text/html
{
    "ETag": ""fd5ff7743e5ed1e1c304eb1c34e8e39f""
}
$ aws s3api head-object --bucket <bucket> --key foo.html
{
    "AcceptRanges": "bytes",
    "ContentType": "text/html",
    "LastModified": "Wed, 15 Apr 2015 06:46:49 GMT",
    "ContentLength": 189,
    "ETag": ""fd5ff7743e5ed1e1c304eb1c34e8e39f"",
    "Metadata": {}
}

虽然它似乎设置了某种特殊的 ContentType 属性,但 AWS 控制台中仍然没有 Content-Type 元数据字段,我也无法访问浏览器中的文件.

While it appears to set some sort of special ContentType property, there still isn't a Content-Type metadata field in the AWS console, nor can I access the file in a browser.

我也尝试过类似的命令(aws s3 cpaws s3 sync),但没有成功.我已将存储桶策略设置为公开可读.

I've also tried similar commands (aws s3 cp, aws s3 sync), with no luck. I have the bucket policy set to publicly-readable.

推荐答案

您使用 --content-type 的第二个示例是为对象设置内容类型的方法.显示的 JSON 响应将 HTTP 响应中的 Content-Type 标头映射到 ContentType 键,但它对应于实际的 Content-Type对象的头部.我确认在使用 --content-type 时,内容类型值确实显示在控制台的元数据部分中.

Your second example with --content-type is the way to set content type for an object. The JSON response displayed is mapping the Content-Type header in the HTTP response to the ContentType key, but it corresponds to the actual Content-Type header of the object. I confirmed that the content type value does show up in the metadata section in the console when using --content-type.

$ aws s3api put-object --bucket bucket --key foo.json --body foo.json --content-type application/json --acl public-read

$ aws s3api head-object --bucket jamesls-test-sync --key foo.json
{
    "AcceptRanges": "bytes",
    "ContentType": "application/json",
    "LastModified": "Wed, 15 Apr 2015 17:18:58 GMT",
    "ContentLength": 0,
    "ETag": ""d41d8cd98f00b204e9800998ecf8427e"",
    "Metadata": {}
}

同样使用curl,我们可以看到设置了内容类型标题:

Also using curl, we can see the content type header is set:

$ curl -I https://bucket.s3.amazonaws.com/foo.json
HTTP/1.1 200 OK
x-amz-id-2: ZlSg1aDUBu7z+9gWUg24uRn2TioI0hk2AGBBZ1iVbpUkv8RTrHWovzbHxL/y21Qe
x-amz-request-id: 8568C73EB95EE5A6
Date: Wed, 15 Apr 2015 17:20:42 GMT
Last-Modified: Wed, 15 Apr 2015 17:18:58 GMT
ETag: "d41d8cd98f00b204e9800998ecf8427e"
Accept-Ranges: bytes
Content-Type: application/json
Content-Length: 0
Server: AmazonS3

这篇关于使用 AWS CLI 上传到 S3 时如何设置 Content-Type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆