itsdax / AWS-Lambda-Image-Compressor-Java

Lambda Function that compresses PNG and JPG files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS-Lambda-Image-Compressor-Java

Lambda Function that compresses PNG and JPG files. Replaces original file in S3 with compressed. Reduces image size while keeping quality loss at minimal. Outputs new file in the original image location of S3.

Instructions:

  1. Install Maven.
brew install maven
  1. Clone this repo.

  2. cd into this repo.

  3. Package as shaded JAR

mvn clean package shade:shade
  1. A target folder with the final JAR artifact will be created. In this case the name of the the JAR artifact will be 'image-compression-1.0-SNAPSHOT.jar'

  2. Create a new Lambda Function on AWS. Make sure to add a role with all the appropiate permissions (S3 read and write, CloudWatch, etc).

  3. Upload shaded JAR.

  4. Set up a S3 trigger for Object creation in your desired bucket-> Object Created (all).

  5. Change Handler Field to:

Index::handleRequest
  1. Increase Memory and Timeout Limit (otherwise function will timeout)

  2. Done. Now whenever a PNG or JPG is uploaded to the bucket with the S3 event added on step 8, this function will get fired and the image will get compressed.

Optional: If you wish to change the compression rate, you can change the value in line 96 of index.java

HappyFace.jpg

Before

Before

After

After

About

Lambda Function that compresses PNG and JPG files.


Languages

Language:Java 100.0%