I'm relatively new to node, and want to write a module that takes an image from an S3 bucket, resizes it and saves it to a temporary directory on Amazon's new Lambda service and then uploads the images back to the bucket.
When I run the code, none of my functions seem to be called (download
, transform
and upload
). I am using tmp
to create the temporary directory and graphicsMagick
to resize the image.
What is wrong with my code?
I have defined the dependencies and the array outside of the module, because I have another which depends on these.
// dependencies
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true });
var fs = require("fs");
var tmp = require("tmp");
// get reference to S3 client
var s3 = new AWS.S3();
var _800px = {
width: 800,
destinationPath: "large"
};
var _500px = {
width: 500,
destinationPath: "medium"
};
var _200px = {
width: 200,
destinationPath: "small"
};
var _45px = {
width: 45,
destinationPath: "thumbnail"
};
var _sizesArray = [_800px, _500px, _200px, _45px];
var len = _sizesArray.length;
exports.AwsHandler = function(event) {
// Read options from the event.
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey = event.Records[0].s3.object.key;
var dstnKey = srcKey;
// create temporary directory
var tmpobj = tmp.dirSync();
// function to determine paths
function _filePath (directory, i) {
if (!directory) {
return "dst/" + _sizesArray[i].destinationPath + "/" + dstnKey;
} else {
return directory + "/dst/" + _sizesArray[i].destinationPath + "/" + dstnKey;
}
};
// 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;
};
(function resizeImage () {
function download () {
console.log("started!");
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
function (err, response) {
if (err) {
console.error(err);
}
// call transform if successful
transform (response);
}
);
};
function transform (response) {
for ( var i = 0; i<len; i++ ) {
// define path for image write
var _Key = _filePath (tmpobj, i);
// resize images
gm(response.Body, srcKey)
.resize(_sizesArray[i].width)
.write(_Key, function (err) {
if (err) {
console.error(err);
}
upLoad ();
});
}
};
function upLoad () {
for ( var i = 0; i<len; i++ ) {
var readPath = _filePath (tmpobj, i);
var writePath = _filePath (i);
// read file from temp directory
fs.readFile(readPath, function (err, data) {
if (err) {
console.error(err);
}
// upload images to s3 bucket
s3.putObject({
Bucket: srcBucket,
Key: writePath,
Body: data,
ContentType: data.type
},
function (err) {
if (err) {
console.error(err);
}
console.log("Uploaded with success!");
});
})
}
// Manual cleanup of temporary directory
tmpobj.removeCallback();
};
}());
};
Aucun commentaire:
Enregistrer un commentaire