Serverless / Lambda + S3: Is it possible to catch the "GetObject" event and select a different resource?

What I'm trying to do is catch any request for an image file and check if that image doesn't exist, return another image.

I am looking at Lambda and serverless platform but I couldn't find much information on this. Is it possible?

+3


source to share


1 answer


There is no GetObject event. Please follow this link for a list of supported events. S3 will only notify you (or call a lambda function) when an object is created, deleted, or lost due to redundancy reduction.

Thus, it is not possible to accomplish what you want, but you have several alternatives.



Alternatives

  • Use Lambda @Edge to intercept your calls to a CloudFront distribution that uses S3 as Origin. This interceptor can send another file if the requested one is missing. This is not a good solution as you would increase the latency and cost of your work.

  • Instead of offering an S3 endpoint to your customers, offer an API gateway endpoint. In this case, ALL image requests are processed by the Lambda function, with the option to provide another file if the requested one is not present. This is not a good solution as you would increase the latency and cost of your work.

  • And the best option that might work, but I haven't tried, is to set up an S3 redirect rule. This is a common use for static website hosting where no page found (status code 404) is redirected to another page (e.g. page-not-found.html). In your case, you can try redirecting to the default image url. This solution will not use lambda functions.

+3


source







All Articles