diff --git a/CreateThumbnail.js b/CreateThumbnail.js index ff6734f..3d7e61a 100644 --- a/CreateThumbnail.js +++ b/CreateThumbnail.js @@ -5,91 +5,90 @@ var gm = require('gm').subClass({ imageMagick: true }); // Enable ImageMagick in var util = require('util'); // constants -var MAX_WIDTH = 100; +var MAX_WIDTH = 100; var MAX_HEIGHT = 100; // get reference to S3 client var s3 = new AWS.S3(); - + exports.handler = function(event, context) { - // Read options from the event. - console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); - var srcBucket = event.Records[0].s3.bucket.name; - // Object key may have spaces or unicode non-ASCII characters. - var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); - var dstBucket = srcBucket + "resized"; - var dstKey = "resized-" + srcKey; + // Read options from the event. + console.log("Reading options from event:\n", util.inspect(event, { depth: 5 })); + var srcBucket = event.Records[0].s3.bucket.name; + // Object key may have spaces or unicode non-ASCII characters. + var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); + var dstBucket = srcBucket + "-resized"; + var dstKey = "resized-" + srcKey; - // Sanity check: validate that source and destination are different buckets. - if (srcBucket == dstBucket) { - console.error("Destination bucket must not match source bucket."); - return; - } + // Sanity check: validate that source and destination are different buckets. + if (srcBucket == dstBucket) { + console.error("Destination bucket must not match source bucket."); + return; + } - // Infer the image type. - var typeMatch = srcKey.match(/\.([^.]*)$/); - if (!typeMatch) { - console.error('unable to infer image type for key ' + srcKey); - return; - } - var imageType = typeMatch[1]; - if (imageType != "jpg" && imageType != "png") { - console.log('skipping non-image ' + srcKey); - return; - } + // Infer the image type. + var typeMatch = srcKey.match(/\.([^.]*)$/); + if (!typeMatch) { + console.error('unable to infer image type for key ' + srcKey); + return; + } + var imageType = typeMatch[1]; + if (imageType != "jpg" && imageType != "png") { + console.log('skipping non-image ' + srcKey); + return; + } - // Download the image from S3, transform, and upload to a different S3 bucket. - async.waterfall([ - function download(next) { - // Download the image from S3 into a buffer. - s3.getObject({ - Bucket: srcBucket, - Key: srcKey - }, - next); - }, - function tranform(response, next) { - gm(response.Body).size(function(err, size) { - // Infer the scaling factor to avoid stretching the image unnaturally. - var scalingFactor = Math.min( - MAX_WIDTH / size.width, - MAX_HEIGHT / size.height - ); - var width = scalingFactor * size.width; - var height = scalingFactor * size.height; + // Download the image from S3, transform, and upload to a different S3 bucket. + async.waterfall([ + function download(next) { + // Download the image from S3 into a buffer. + s3.getObject({ + Bucket: srcBucket, + Key: srcKey + }, + next); + }, + function tranform(response, next) { + gm(response.Body).size(function(err, size) { + // Infer the scaling factor to avoid stretching the image unnaturally. + var scalingFactor = Math.min( + MAX_WIDTH / size.width, + MAX_HEIGHT / size.height + ); + var width = scalingFactor * size.width; + var height = scalingFactor * size.height; - // Transform the image buffer in memory. - this.resize(width, height) - .toBuffer(imageType, function(err, buffer) { - if (err) { - next(err); - } else { - next(null, response.ContentType, buffer); - } - }); - }); - }, - function upload(contentType, data, next) { - // Stream the transformed image to a different S3 bucket. - s3.putObject({ - Bucket: dstBucket, - Key: dstKey, - Body: data, - ContentType: contentType - }, - next); - } - ], function (err) { - if (err) { - msg = 'Unable to resize ' + srcBucket + '/' + srcKey + - ' and upload to ' + dstBucket + '/' + dstKey + - ' due to an error: ' + err; - } else { - msg = 'Successfully resized ' + srcBucket + '/' + srcKey + - ' and uploaded to ' + dstBucket + '/' + dstKey; - } + // Transform the image buffer in memory. + this.resize(width, height) + .toBuffer(imageType, function(err, buffer) { + if (err) { + next(err); + } else { + next(null, response.ContentType, buffer); + } + }); + }); + }, + function upload(contentType, data, next) { + // Stream the transformed image to a different S3 bucket. + s3.putObject({ + Bucket: dstBucket, + Key: dstKey, + Body: data, + ContentType: contentType + }, + next); + } + ], function(err) { + if (err) { + msg = 'Unable to resize ' + srcBucket + '/' + srcKey + + ' and upload to ' + dstBucket + '/' + dstKey + + ' due to an error: ' + err; + } else { + msg = 'Successfully resized ' + srcBucket + '/' + srcKey + + ' and uploaded to ' + dstBucket + '/' + dstKey; + } - context.done(err, msg); - } - ); -}; + context.done(err, msg); + }); +}; \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index 10d2d59..f892e1e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,23 +2,11 @@ var grunt = require('grunt'); grunt.loadNpmTasks('grunt-aws-lambda'); grunt.initConfig({ - lambda_invoke: { - default: { - options: { - file_name: 'CreateThumbnail.js' - } - } - }, - lambda_deploy: { - default: { - arn: 'arn:aws:lambda:us-east-1:1234567890123:function:CreateThumbnail' - } - }, - lambda_package: { - default: { - include_time: false - } - } -}); - -grunt.registerTask('deploy', ['lambda_package', 'lambda_deploy']) + lambda_package: { + default: { + options: { + include_time: false + } + } + } +}); \ No newline at end of file diff --git a/package.json b/package.json index f276fc5..cda9b92 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,21 @@ { - "name": "create-thumbs-lambda", - "version": "0.0.2", - "description": "AWS Lambda function to create an image thumbnail", - "main": "CreateThumbnail.js", - "dependencies": { - "gm": "^1.17.0", - "async": "^0.9.0" - }, - "devDependencies": { - "aws-sdk": "latest", - "grunt": "0.4.*", - "grunt-aws-lambda": "0.8.0", - "npm": "^2.8.3" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "Amazon Web Services Inc.", - "license": "Apache 2.0" - } + "name": "create-thumbs-lambda", + "version": "0.0.4", + "description": "AWS Lambda function to create an image thumbnail", + "main": "CreateThumbnail.js", + "dependencies": { + "gm": "^1.17.0", + "async": "^0.9.0" + }, + "devDependencies": { + "aws-sdk": "latest", + "grunt": "0.4.*", + "grunt-aws-lambda": "0.8.0", + "npm": "^2.8.3" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Amazon Web Services Inc.", + "license": "Apache 2.0" +} \ No newline at end of file