How to redirect s3 content request to lambda?

I am following this tutorial to implement a sketch on the fly with s3 and lambda. I am stuck in redirecting non-existent request to lambda

My s3 container is private and I use a pre-signed URL with a Cognito idtoken to access its content, it works.

Now, if I make a request for a thumbnail that has not been created (does not exist in s3), I want s3 to redirect the request to lambda. I can’t figure out how to do this.

here are the s3 role

<RoutingRules>
  <RoutingRule>
  <Condition>
  <KeyPrefixEquals/>
  <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
</Condition>
<Redirect>
  <Protocol>https</Protocol>
  <HostName>MY_LAMBDA_DOMAIN</HostName>
  <ReplaceKeyPrefixWith>dev/photos?key=</ReplaceKeyPrefixWith>
  <HttpRedirectCode>307</HttpRedirectCode>
</Redirect>

  • when I access a non-existent thumbnail with a pre-signed URL, it gives me a 403 error, so I'm trying to pre-sign the URL with the s3 endpoint

    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
    })
    

    404 enter image description here

  • , , , s3. , .

  • - s3?

,

+4
1

, S3. S3 .

, , Cloudfront Lambda @Edge.

: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html

, S3 Cloudfront, : ": Origin-Response 302-". 403 404 , URL-, , .

+3

Source: https://habr.com/ru/post/1694576/


All Articles