Get Amazon S3 to redirect to my endpoint on missing object requests?

amazon-s3, http, redirect

Solution

You can use `RoutingRules` to have S3 redirect to your domain as described in the Example 3 of Configure a Bucket for Website Hosting from the S3 documentation. I believe it describes your situation accurately.

<RoutingRules>
  <RoutingRule>
  <Condition>
    <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
  </Condition>
  <Redirect>
    <HostName>mydomain.com</HostName>
    <ReplaceKeyPrefixWith>missing_s3_obj/bucket1/</ReplaceKeyPrefixWith>
    <HttpRedirectCode>307</HttpRedirectCode>
  </Redirect>
  </RoutingRule>
</RoutingRules>

Problem

We use S3 for online storage of files. To reduce costs, many of those files can be generated on demand with a path known ahead of time. Is it possible to get S3 bucket to redirect missing object requests to my pre-configured endpoint (which can then generate and serve the file on demand)? For example, a request to http://bucket1.s3.amazonaws.com/path2/file3.jpg would temporarily redirect (307) to http://mydomain.com/missing_s3_obj/bucket1/path2/file3.jpg.

Original source