S3 object lambda access point thumbnail generation

Hello, currently working on solution to generate thumbnail of images/pdf from S3 objects without the need to download the object resize it then push the thumbnail to a different bucket, is this possible to be done on the fly via object access lambda access point and does it support multiple objects? Can someone shed some light how the architecture of such implementation might look like?

There’s a blog post on it here, which specifically calls out resizing/watermarking images as one possible use case: https://aws.amazon.com/blogs/aws/introducing-amazon-s3-object-lambda-use-your-code-to-process-data-as-it-is-being-retrieved-from-s3/

It’s done as part of the GET request for each individual object, so each Lambda invocation would only process one object.

Will each object only be accessed once? If not, then it might be worth looking at delivering them via CloudFront/Lambda@Edge if that fits into your architecture, as you’d be able to cache the processed objects that way.

I think most likely I’m looking for something that might require to process more than one object,and ideally I would like to use Java SDK as part of the microservice rather than lambda.

Java is a supported runtime for Lambda (with the exception of Lambda@Edge, but sounds like that isn’t of interest anyway).

If you want to process multiple objects then you’d be looking at S3 event notifications to SQS which could then be consumed in batches by Lambda or anything else. This is obviously done when objects are written though, not on the fly when requested as per your original question.

The need to do it on the fly when objects are requested but also in batches seem slightly at odds with each other though - can you explain the use case a little more?

Sure, so the use case is the client would list the images or PDFs from S3 via cloudfront, what I would like to achieve is show the thumbnail preview and when users hover over it show the full sized preview.
Now that I’m reading your reply I think lambda edge sounds like the way to go but correct me if you have a better thought for this.

Yes - assuming you are ok to write your function in Python or NodeJS rather than Java then I think Lambda@Edge would be a good fit for your use case.

Here are a couple of blog posts which might be of interest:

https://aws.amazon.com/blogs/networking-and-content-delivery/resizing-images-with-amazon-cloudfront-lambdaedge-aws-cdn-blog/

https://mobilereality.pl/blog/creating-thumbnails-on-the-fly-with-s3-cloudfront-lambdaedge-and-terraform

Thanks, I have some doubts regarding the cloudfront cache, say I have if have around 1GB of attachments of images/PDFs..etc, is it a sustainable solution to cache such huge amount of data?

There are no quotas around the total amount of data which CF will cache - there is a default TTL of 24 hours but this can be overridden by setting the relevant headers in the origin response.

Note that in some cases objects which are not frequently requested may be evicted before their TTL expires, but there are no specific rules documented for this (the documentation simply states “If a file in an edge location isn’t frequently requested, CloudFront might evict the file - remove the file before its expiration date - to make room for files that have been requested more recently.”)

This page goes into more detail: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html

Thanks, stumbled upon this tutorial (link below) which seems to be working perfectly fine for the use case, with exception that it only loads images , how do you go with files extensions which got not needed for resizing like PDF or Zip? Should they be stored in a separate S3 bucket?

https://aws.amazon.com/blogs/networking-and-content-delivery/image-optimization-using-amazon-cloudfront-and-aws-lambda/

You can store them in the same or different buckets, but I would associate a specific behaviour with the images in CloudFront based on a specific path, i.e. one behaviour for /images/* (or similar) and the default behaviour (*) to catch everything else. Since Lambda@Edge functions are associated with behaviours, in this way you can ensure that you only execute the function for image requests.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesPathPattern

I’m experiencing a very strange behaviour, so I left behaviour default as is, and i added /files/* in which it won’t trigger the lambdaEdge, the issue is now I can only access files which are .PDF but for any other extension like .zip or so I get access denied from cloudfront, do you doubt something in the setup?

So you can’t access anything under /files, but everything else is ok?

If so my guess would be that those objects are not stored under /files in your origin S3 bucket and can’t be found (CF will pass the request path through to the origin) - S3 will return a 403 rather than a 404 in the case of a missing object unless the client accessing the bucket (CF in this case) has ListBucket permissions in addition to GetObject permissions, which is not normally the case.

Alternatively, if the path is correct and these objects are stored in a different bucket to the others it could simply be a case of an incorrect bucket policy not allowing GetObject permissions to CF.

I can access files which are of .pdf extension and not any other type, which seems to be odd, cause CF would block all files under /files/* if access is denied

So some files under /files are working and others not, and they are all stored under the same prefix in the same S3 bucket?

All I can suggest is double checking the object names you are requesting are exactly right (since a 404 can manifest itself as a 403 as described above) as well as checking the bucket policy and object permissions for anything weird.