Skip to content

Instantly share code, notes, and snippets.

@li0nel
Last active September 25, 2016 06:11
Show Gist options
  • Save li0nel/0c3d43a70a64324de707 to your computer and use it in GitHub Desktop.
Save li0nel/0c3d43a70a64324de707 to your computer and use it in GitHub Desktop.
Generates image thumbnails using AWS Lambda
var async = require("async");
var AWS = require("aws-sdk");
var im = require("gm").subClass({imageMagick: true});
var s3 = new AWS.S3();
var path = require("path");
var CONFIG = require("./config.json");
function getImageType(objectContentType) {
if (objectContentType === "image/jpeg") {
return "jpeg";
} else if (objectContentType === "image/png") {
return "png";
} else if (objectContentType === "image/gif") {
return "gif";
} else if (objectContentType === "image/svg+xml") {
return "svg";
} else {
throw new Error("unsupported objectContentType " + objectContentType);
}
}
exports.handler = function(event, context) {
console.log("event ", JSON.stringify(event));
async.mapLimit(event.Records, CONFIG.concurrency, function(record, cb) {
var originalKey = decodeURIComponent(record.s3.object.key.replace(/\+/g, " "));
s3.getObject({
"Bucket": record.s3.bucket.name,
"Key": originalKey
}, function(err, data) {
if (err) {
cb(err);
} else {
cb(null, {
"originalKey": originalKey,
"contentType": data.ContentType,
"imageType": getImageType(data.ContentType),
"buffer": data.Body,
"record": record
});
}
});
}, function(err, images) {
if (err) {
context.fail(err);
} else {
async.eachLimit(images, CONFIG.concurrency, function(image, cb) {
var sizes = [["/", "432x"], ["/thumbs/", "52x"]];
async.each(sizes, function(size, cb) {
if (image.imageType !== "image/svg+xml") {
if (size[0].indexOf("/thumbs/") > -1)
im(image.buffer).quality(99).strip().sharpen(6, 3).resize(size[1], "", "^>").toBuffer(image.imageType, function (err, buffer) {
if (err) {
cb(err);
} else {
s3.putObject({
"Bucket": image.record.s3.bucket.name,
"Key": (path.dirname(image.originalKey) + size[0] + path.basename(image.originalKey)).replace("originals/", ""),
"Body": buffer,
"ContentType": image.contentType
}, function (err) {
cb(err);
});
}
});
else
im(image.buffer).quality(99).strip().resize(size[1], "", "^>").toBuffer(image.imageType, function (err, buffer) {
if (err) {
cb(err);
} else {
s3.putObject({
"Bucket": image.record.s3.bucket.name,
"Key": (path.dirname(image.originalKey) + size[0] + path.basename(image.originalKey)).replace("originals/", ""),
"Body": buffer,
"ContentType": image.contentType
}, function (err) {
cb(err);
});
}
});
} else {
s3.putObject({
"Bucket": image.record.s3.bucket.name,
"Key": (path.dirname(image.originalKey) + size[0] + path.basename(image.originalKey)).replace("originals/", ""),
"Body": image.buffer,
"ContentType": image.contentType
}, function (err) {
cb(err);
});
}
});
}, function(err) {
if (err) {
context.fail(err);
} else {
context.succeed();
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment