DEV Community

Rahul
Rahul

Posted on

How to perform realtime transformations on S3

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.

Image description

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

  1. Compressing or decompressing files as they are being downloaded.
  2. Resizing and watermarking images on the fly.
  3. Converting across data formats, such as converting XML to JSON.
  4. Implementing custom authorization rules to access data. Many more (left to the devs’ creativity).

Steps

  1. Create an Access Point for S3 bucket.
  2. Create an Object Lambda Access Point, and attach above created Access Point and a Lambda, with required permissions like GetObject.
  3. Write a code in above created Lambda, to retrive and modify the files according to your needs. Example Code

Create an S3 Object Lambda Access Point from the S3 Management Console.

Create an Object Lambda Access Point from S3 Console<br>

Access Point and Lambda inputs, with required permissions like GetObject

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

Blog of Codes | Rahul | Substack

Articles about cloud architecture and programming. Click to read Blog of Codes, by Rahul, a Substack publication. Launched 2 months ago.

favicon blogofcodes.substack.com

You can find me on Twitter and LinkedIn ✌️

Top comments (0)