Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the content-type of an object using AWS CLI?

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?

like image 524
nelstrom Avatar asked May 08 '14 17:05

nelstrom


People also ask

How do I change my AWS CLI AWS?

To switch between different AWS accounts, set the AWS_profile environment variable at the command line via export AWS_PROFILE=profile_name . Setting the env variable changes the default profile until the end of your shell session or until you set the variable to a different value.

Can you edit an S3 object?

You can use the Amazon S3 console to edit metadata of existing S3 objects.

Which CLI command is used to upload content to an S3 bucket?

Use the s3 cp command to copy objects from a bucket or a local directory.


Video Answer


2 Answers

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!

like image 186
Cawb07 Avatar answered Oct 25 '22 08:10

Cawb07


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 
like image 36
Malvineous Avatar answered Oct 25 '22 08:10

Malvineous