Learn a new feature of AWS S3 which can be used to do realtime transformations like compression, resizing images etc.
Let’s discuss a new feature of S3, S3 Object Lambda, which can do realtime transformations of files, as you retrieve them.
Recently came across an S3 feature, where you can attach lambda to any AWS S3 bucket and do your conversions, transformations, altering on the fly and also use all the features that S3+CloudFront provides.
Use cases
- Compressing or decompressing files as they are being downloaded.
- Resizing and watermarking images on the fly.
- Converting across data formats, such as converting XML to JSON.
- Implementing custom authorization rules to access data. Many more (left to the devs’ creativity).
Steps
- Create an Access Point for S3 bucket.
- Create an Object Lambda Access Point, and attach above created Access Point and a Lambda, with required permissions like GetObject.
- Write a code in above created Lambda, to retrive and modify the files according to your needs. Example Code
Example
Let’s use the example of image transformation on the fly.
- User asks for sunset_600x400.jpg
- Lambda function will get invoked.
- If sunset_600x400.jpg is available, return 200 success to continue downloading the file.
- If sunset_600x400.jpg is not available, will remove suffix _600x400 and look for an image named sunset.jpg
- Resize it to fit the maximum width and height as described in the file name and save it in S3 as sunset_600x400.jpg
- Return 200 success to continue downloading the file.
Caveats
For every S3 file access, lambda will get invokes and thus need to pay extra for Lambda.
In Conclusion…
With this new feature from AWS, you can do file transformations, image resize/compressions, limit access etc on the fly.
Let me know if you find any other use cases.
That’s it! Please let me know about your views and comment below for any clarifications.
If you found value in reading this, please consider sharing it with your friends and also on social media 🙏
Also, to be notified about my upcoming articles, subscribe to my newsletter below (I’ll not spam you 😂)
Top comments (0)