Frew Schmidt
Frew Schmidt

Reputation: 9544

How do I set metadata with aws s3api?

I'm trying to make my CloudFront hosted blog redirect /feed/atom/index.html to /index.xml. I have the following script that is supposed to set up redirect headers for me:

#!/bin/sh

redirect() aws s3api copy-object                 \
   --copy-source blog.afoolishmanifesto.com$1    \
   --bucket blog.afoolishmanifesto.com --key $1  \
   --metadata x-amz-website-redirect-location=$2 \
   --metadata-directive REPLACE

redirect /feed/atom/index.html /index.xml

After running the script I get the following output:

{
    "CopyObjectResult": {
        "LastModified": "2016-03-27T07:26:03.000Z", 
        "ETag": "\"40c27e3a5ea160c6695d7f34de8b4dea\""
    }
}

And when I refresh the object in the AWS console view of S3 I do not see a Website Redirect Location (or x-amz-website-redirect-location) piece of metadata for the object in question. What can I do to ensure that the redirect is configured correctly?

Note: I have tried specifying the metadata as JSON and as far as I can tell it made no difference.

UPDATE: I have left the above question the same, as it still applies to metadata, but if you are trying to create a redirect with aws s3api you should use the --website-redirect-location option, not --metadata.

Upvotes: 0

Views: 1101

Answers (1)

helloV
helloV

Reputation: 52375

It is not able to create a key /feed/atom/index.html in the bucket, so no metadata attribute was not created. Instead you should create feed/atom/index.html. I'll modify it like:

#!/bin/sh

redirect() aws s3api copy-object                 \
   --copy-source blog.afoolishmanifesto.com/$1    \
   --bucket blog.afoolishmanifesto.com --key $1  \
   --metadata x-amz-website-redirect-location=$2 \
   --metadata-directive REPLACE

redirect feed/atom/index.html /index.xml

In my solution, notice / in --copy-source and the first argument to redirect script missing the leading /

Upvotes: 1

Related Questions