Skip to content

Instantly share code, notes, and snippets.

@joeangel
Created August 19, 2015 06:27
Show Gist options
  • Save joeangel/6c03aa5101f7b4dcb262 to your computer and use it in GitHub Desktop.
Save joeangel/6c03aa5101f7b4dcb262 to your computer and use it in GitHub Desktop.
aws lambda shrink image test
console.log('Loading function');
var async = require("async");
var aws = require('aws-sdk');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
var gm = require("gm").subClass({imageMagick: true});
var fs = require("fs");
var mktemp = require("mktemp");
var THUMB_KEY_PREFIX = "lambda-test/",
THUMB_WIDTH = 100,
THUMB_HEIGHT = 100,
ALLOWED_FILETYPES = ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'pdf', 'gif'];
var utils = {
decodeKey: function(key) {
return decodeURIComponent(key).replace(/\+/g, ' ');
}
};
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var srcKey = utils.decodeKey(event.Records[0].s3.object.key);
var dstKey = THUMB_KEY_PREFIX + srcKey.replace(/\.\w+$/, ".jpg");
var fileType = srcKey.match(/\.\w+$/);
var params = {
Bucket: bucket,
Key: srcKey
};
s3.getObject(params, function(err, data) {
if (err) {
console.log(err);
var message = "Error getting object " + srcKey + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.";
console.log(message);
context.fail(message);
} else {
console.log('CONTENT TYPE:', data.ContentType);
console.log('Bucket:', bucket);
console.log('srcKey:', srcKey);
console.log('dstKey:', dstKey);
console.log('fileType:', fileType);
context.succeed(data.ContentType);
}
});
async.waterfall(
[
// Download the image from S3
function download(next) {
console.log('in function:', 'download');
s3.getObject({
Bucket: bucket,
Key: srcKey
}, next);
},
// Shrink the image
function createThumbnail(response, next) {
console.log('in function:', 'createThumbnail');
var temp_file, image;
if(fileType === "pdf") {
temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.pdf")
fs.writeFileSync(temp_file, response.Body);
image = gm(temp_file + "[0]");
} else if (fileType === 'gif') {
temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.gif")
fs.writeFileSync(temp_file, response.Body);
image = gm(temp_file + "[0]");
} else {
image = gm(response.Body);
}
image.size(function(err, size) {
var scalingFactor = Math.min(THUMB_WIDTH / size.width, THUMB_HEIGHT / size.height),
width = scalingFactor * size.width,
height = scalingFactor * size.height;
this.resize(width, height)
.toBuffer("png", function(err, buffer) {
if(temp_file) {
fs.unlinkSync(temp_file);
}
if (err) {
next(err);
} else {
next(null, response.contentType, buffer);
}
});
});
},
function uploadThumbnail(contentType, data, next) {
console.log('in function:', 'uploadThumbnail');
s3.putObject({
Bucket: bucket,
Key: dstKey,
Body: data,
ContentType: "image/png",
ACL: 'public-read',
Metadata: {
thumbnail: 'TRUE'
}
}, next);
}
],
function(err) {
if (err) {
console.error(
"Unable to generate thumbnail for '" + bucket + "/" + srcKey + "'" +
" due to error: " + err
);
} else {
console.log("Created thumbnail for '" + bucket + "/" + srcKey + "'");
}
context.done();
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment