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

nelstrom picture nelstrom · May 8, 2014 · Viewed 11.9k times · Source

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?

Answer

Cawb07 picture Cawb07 · Feb 3, 2015

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!