Serverless / Lambda + S3: Is it possible to catch the "GetObject" event and select a different resource?
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.
source to share