After completing the AWS Lambda tutorial for creating thumbnails, I decided to try and tweak the code to check if a file was a jpg or csv file and if it was simply move it to a new bucket. The only things I removed from my code were the comments and the function within the async.waterfall that would resize images. However, whenever I test or run this new code, I get "process exited before completing request" and the function does not transfer the files correctly. Here is the code:
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm')
.subClass({ imageMagick: true }); // Enable ImageMagick integration.
var util = require('util');
var s3 = new AWS.S3();
exports.handler = function(event, context) {
console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey =
decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var dstBucket = 'datacollectionbucket';
var dstKey = srcKey;
if (srcBucket == dstBucket) {
console.error("Destination bucket must not match source bucket.");
return;
}
var typeMatch = srcKey.match(/\.([^.]*)$/);
if (!typeMatch) {
console.error('unable to infer file type for key ' + srcKey);
return;
}
var imageType = typeMatch[1];
if (imageType != "jpg" && imageType != "csv") {
console.log('skipping unrecognized file ' + srcKey);
return;
}
async.waterfall([
function download(next) {
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
function upload(contentType, data, next) {
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
}
], function (err) {
if (err) {
console.error(
'Unable to resize ' + srcBucket + '/' + srcKey +
' and upload to ' + dstBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully classified ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey
);
}
context.done();
}
);
};
Thanks guys
Aucun commentaire:
Enregistrer un commentaire