19

我有几个对象存储在 Amazon S3 中,我需要将其内容类型从更改text/htmlapplication/rss+xml. 我认为应该可以使用复制命令来执行此操作,为源和目标指定相同的路径。我正在尝试使用 AWS cli 工具执行此操作,但出现此错误:

$ 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.

如果我为源和目标指定不同的路径,我不会收到错误消息:

$ 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

即使命令成功完成,index2.html对象也是使用text/html内容类型创建的,而不是application/rss+xml我指定的类型。

如何修改此命令行以使其正常工作?

4

3 回答 3

15

可以使用低级别s3api进行此更改:

$ 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

问题只是无法指定--metadata-directive. 感谢您指出未解决的问题/功能请求,nelstrom!

于 2015-02-02T23:54:36.717 回答
8

您也可以使用更高级别的 API 来实现,方法是将文件复制到自身之上,但将其标记为元数据中的更改:

aws s3 cp \
  --content-type "application/rss+xml" \
  --metadata-directive REPLACE \
  s3://mybucket/myfile \
  s3://mybucket/myfile 
于 2018-11-25T04:05:05.010 回答
1

您可以使用命令覆盖文件的内容类型aws s3 cp,使用--metadata-directive可选属性指定内容类型被替换--content-type 'application/rss+xml'为复制期间提供的内容类型:

aws s3 cp \
--content-type 'application/rss+xml' \
--metadata-directive REPLACE \
s3://mybucket/feed/ogg/index.html \
s3://mybucket/feed/ogg/index.html

更多信息:https ://docs.aws.amazon.com/cli/latest/reference/s3/cp.html

然后,您可以通过检查文件元数据来验证它:

aws s3api head-object \
--bucket mybucket \
--key feed/ogg/index.html
于 2021-01-06T13:17:00.773 回答