3

I want to use redirects with S3 webpages. The AWS Configuring a Web Page Redirect document says to set the x-amz-website-redirect-location property, but this doesn't seem to be settable. If I try to set the key, S3 prefixes the key with x-amz-meta- (making it a user-defined metadata) and the modified key doesn't cause a redirect.

I set the metadata with the aws CLI:

 aws s3api copy-object --copy-source static.righto.com/zero \
--bucket static.righto.com --key bar \
--metadata x-amz-website-redirect-location=http://righto.com/bar \
--metadata-directive REPLACE

but when I look in the S3 console, the metadata name has been prefixed and the redirect is ignored.

The same thing happens if I use the Python API:

s3.Bucket('static.righto.com').put_object(Key=src, Body='',
  Metadata={'x-amz-website-redirect-location': dst})

And if I try to set the x-amz-website-redirect-location metadata key in the S3 console, I get the error "User-defined metadata keys must start with x-amz-meta-."

Did x-amz-website-redirect-location become unsupported?

If I set "Website Redirect Location" metadata in the console then redirects work, so issue isn't the redirect endpoint problems in this answer. But I can't use "Website Redirect Location" as a metadata key in the API.

Community
  • 1
  • 1
Ken Shirriff
  • 1,433
  • 14
  • 16

1 Answers1

5

Use --website-redirect-location <value>.

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

x-amz-website-redirect-location is not user metadata (x-amz-meta-*), which is what the --metadata option is used for.

Michael - sqlbot
  • 139,456
  • 21
  • 252
  • 328
  • I'm confused whether I just need to set that (and on what command, copy? I'm not copying my website build folder for example, it's already been created) or if I need routing _rules_) – PositiveGuy Jul 11 '17 at 23:09
  • @PositiveGuy OP was using an empty (0 byte) object he created in the bucket and named `zero`, making a copy of it with the added metadata to the location where he wanted a redirect. There is some overlap between this functionality and routing rules. It depends on what you are trying to accomplish, in how many places, and whether there are patterns involved. – Michael - sqlbot Jul 12 '17 at 00:11
  • for boto, you can check this link: `https://www.designingforthefuture.com/boto3` Basically, you've to do `S3.put_object(Bucket=, Key=, Body='', WebsiteRedirectLocation='http://example.com')` – pyofey Aug 28 '19 at 17:40
  • For those trying to solve this using the Java SDK: https://stackoverflow.com/a/61920902/2162886 – Techmag May 20 '20 at 21:09