How can I change the content-type of an object using aws cli?

…衆ロ難τιáo~ 提交于 2019-11-30 22:22:45

问题


I've got several objects stored in Amazon S3 whose content-type I need to change from text/html to application/rss+xml. I gather that it should be possible to do this with a copy command, specifying the same path for the source and destination. I'm trying to do this using the AWS cli tools, but I'm getting this error:

$ aws s3 cp s3://mybucket/feed/ogg/index.html \
            s3://mybucket/feed/ogg/index.html \
            --content-type 'application/rss+xml'
copy failed: s3://mybucket/feed/ogg/index.html
to s3://mybucket/feed/ogg/index.html
A client error (InvalidRequest) occurred when calling the
CopyObject operation: This copy request is illegal because it is
trying to copy an object to itself without changing the object's
metadata, storage class, website redirect location or encryption
attributes.

If I specify a different path for source and destination, I don't get the error:

$ aws s3 cp s3://mybucket/feed/ogg/index.html \
            s3://mybucket/feed/ogg/index2.html \
            --content-type 'application/rss+xml'
copy: s3://mybucket/feed/ogg/index.html
to s3://mybucket/feed/ogg/index2.html

Even though the command completes successfully, the index2.html object is created with the text/html content type, not the application/rss+xml type that I specified.

How can I modify this command-line to make it work?


回答1:


It's possible to use the low level s3api to make this change:

$ aws s3api copy-object --bucket archive --content-type "application/rss+xml" \
    --copy-source archive/test/test.html --key test/test.html \
    --metadata-directive "REPLACE"

http://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html

The problem was just not being able to specify the --metadata-directive. Thanks for pointing out the open issue / feature request, nelstrom!




回答2:


You can also do it with the higher level API, by copying a file over itself but marking it as a change in metadata:

aws s3 cp \
  --content-type "application/rss+xml" \
  --metadata-directive REPLACE \
  s3://mybucket/myfile \
  s3://mybucket/myfile 


来源:https://stackoverflow.com/questions/23548256/how-can-i-change-the-content-type-of-an-object-using-aws-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!