Resizing image when uploading to s3 by Lambda
When you are building a website it’s important to optimize your photos and it becomes even crucial if you let users upload photos to your application.
What i want
I want users to upload photos in our application and let our lambda to make small photos.
When users upload a photo to s3 a lambda named resizedImage will trigger and it will resize the image and write it back to s3.
In above lambda we receive s3 object in Records and comments in the code are self explaining.
2. Trigger Lambda
In our serverless.yml file add following to attach a trigger.
so event is controller that controls what action will trigger s3, actions can be put, remove, post etc and
if you simply reading and writing images to s3 it can go in infinite loop(i.e to timeout) since lambda will triggered in each writes so here prefix comes into play that will detect the file or key and only trigger s3 if the uploaded image has key upload/.
See first picture code, we are writing resized image in key that start with resized/ when prefix checks in it find resized/ so lambda won't triggered again.
existing: true tells not to create bucket again.