queirozsc
8/30/2017 - 4:32 PM

Automating Image Compression Using S3 & Lambda https://www.aaronmedacco.com/blog/post/2017/07/01/automating-image-compression-using-s3-and-l

var AWS = require("aws-sdk");
var IM = require('imagemagick');
var FS = require('fs');
var compressedJpegFileQuality = 0.80;
var compressedPngFileQuality = 0.95;
 
exports.handler = (event, context, callback) => {
    var s3 = new AWS.S3();
    var sourceBucket = "Source Bucket Name";
    var destinationBucket = "Destination Bucket Name";
    var objectKey = event.Records[0].s3.object.key;
    var getObjectParams = {
        Bucket: sourceBucket,
        Key: objectKey
    };
    s3.getObject(getObjectParams, function(err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            console.log("S3 object retrieval get successful.");
            var resizedFileName = "/tmp/" + objectKey;
            var quality;
            if (resizedFileName.toLowerCase().includes("png")){
                quality = compressedPngFileQuality;
            }
            else {
                quality = compressedJpegFileQuality;
            }
            var resize_req = { width:"100%", height:"100%", srcData:data.Body, dstPath: resizedFileName, quality: quality, progressive: true, strip: true };
            IM.resize(resize_req, function(err, stdout) {
                if (err) {
                    throw err;
                }
                console.log('stdout:', stdout);
                var content = new Buffer(FS.readFileSync(resizedFileName));
                var uploadParams = { Bucket: destinationBucket, Key: objectKey, Body: content, ContentType: data.ContentType, StorageClass: "STANDARD" };
                s3.upload(uploadParams, function(err, data) {
                    if (err) {
                        console.log(err, err.stack);
                    } else{
                        console.log("S3 compressed object upload successful.");
                    }
                });
            });
        }
    });
};
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "Your Source Bucket ARN/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "Your Destination Bucket ARN/*"
            ]
        }
    ]
}