title: How to redirect s3 content request to lambda?
body: <p>I'm following <a href="https://aws.amazon.com/cn/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/" rel="noreferrer">this tutorial</a> to implement generate thumbnail on fly with s3 and lambda. I'm stuck on redirect non-exist request to lambda</p>
  
  <p>My s3 bucket is private, and I use pre-signed url with Cognito idtoken to access its content, it works.</p>
  
  <p>Now if I make a request to a thumbnail that hasn't been generated(not exist in s3), I want s3 to redirect the request to lambda. I can not figure out how to do this. </p>
  
  <p>here is the s3 routeing roles</p>
  
  <pre><code>&lt;RoutingRules&gt;
    &lt;RoutingRule&gt;
    &lt;Condition&gt;
    &lt;KeyPrefixEquals/&gt;
    &lt;HttpErrorCodeReturnedEquals&gt;404&lt;/HttpErrorCodeReturnedEquals&gt;
  &lt;/Condition&gt;
  &lt;Redirect&gt;
    &lt;Protocol&gt;https&lt;/Protocol&gt;
    &lt;HostName&gt;MY_LAMBDA_DOMAIN&lt;/HostName&gt;
    &lt;ReplaceKeyPrefixWith&gt;dev/photos?key=&lt;/ReplaceKeyPrefixWith&gt;
    &lt;HttpRedirectCode&gt;307&lt;/HttpRedirectCode&gt;
  &lt;/Redirect&gt;
  </code></pre>
  
  <p>
  </p>
  
  <ol>
  <li><p>when I access a non-exist thumbnail with pre-signed URL, it gives me 403 error, so I try to pre-signed the url with s3 end point </p>
  
  <pre><code>const s3 = new AWS.S3({endpoint: 'http://s3-website.us-east-2.amazonaws.com'})
  s3.getSignedUrl('getObject', {
    Bucket: BUCKET,
    Key: 'userId/test-thumb.jpg',
    Expires: signedUrlExpireSeconds
  })
  </code></pre>
  
  <p>this time it give me 404 and error message and still not redirect request to lambda
  <a href="https://i.stack.imgur.com/THQFY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/THQFY.png" alt="enter image description here"></a></p></li>
  <li><p>I notice that in the tutorial I mentioned above, it use public s3 bucket.
  I'm not sure is it possible to implement this solution in a private bucket.</p></li>
  <li><p>Does it has anything to do with s3 policy? currently no policy is attached to my private bucket</p></li>
  </ol>
  
  <p>Any suggestion will be appreciated, thanks</p>