How to set Content-Type when booting to S3 using AWS CLI?

I am trying to set up static hosting for S3 websites, but it returns 403 to any of my objects that do not have the Content-Type metadata field set in the AWS console. I cannot figure out how to do this using the AWS CLI tool.

Using the --metadata parameter, a message appears:

 $ 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" } } 

But the Content-Type field of the object does not appear in the "Metadata" section of the AWS console, and I get 403 when I try to access the file in the browser.

Using the --content-type option also does not 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": {} } 

While it seems that some special property ContentType , the AWS console still does not have a Content-Type metadata field, nor can I access the file in the browser.

I also tried similar commands ( aws s3 cp , aws s3 sync ), no luck. My bucket policy is set to public.

+12
amazon-s3 amazon-web-services aws-cli
source share
3 answers

The second example with --content-type is a way to set the content type for an object. The selected JSON response displays the Content-Type header in the HTTP response to the ContentType key, but matches the actual Content-Type header of the object. I have confirmed that the content type value is displayed in the metadata section of 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": {} } 

Also, using curl, we can see that 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 
+16
source share

In the AWS console, you can specify the type of content you want, even if the content type is not listed in the drop-down list.

+3
source share

This is not what the OP is looking for, but Google sends a lot of people here.

If you want to set the content type of an object that is already on S3 without having to load --body again, see --body How to Change the Content Type of Amazon S3 Objects.

0
source share

All Articles