I have been trying to create a AWS node lambda function to download a PDF file from S3, generate a thumbnail for the first page of that file, and upload that thumbnail back to S3. Since I am no expert I tried inspiring myself from the Lambda example provide by AWS to resize images as well as a node.js found on SO of a PDF thumbnail generator in node.js but have not been able to make it work. The download from S3 works, the upload back to S3 works, but the thumbnail generation fails. See my code below:
// Download the pdf from S3, create thumbnail, and upload to cache.
async.waterfall([
function download(next) {
// Download the pdf from S3 into a buffer.
s3.getObject({
Bucket: BUCKET,
Key: pdfkey
},
next);
},
function thumbnail(response, next) {
gm(response.Body[0]).size(function(err, size) {
// Transform the image buffer in memory.
this.resize(requestedwidth, requestedheight).toBuffer(format.toUpperCase(), function(err, buffer) {
if (err) {
console.log('failed generating thumbnail');
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the thumbnail
s3.putObject({
Bucket: BUCKET,
Key: thumbnailkey,
ACL:"public-read",
Body: data,
ContentType: contentType
},
next);
}
], function (err) {
if (err) {
context.fail(new Error(
'Unable to create thumbnail for ' + BUCKET + '/' + pdfkey +
' and upload to ' + BUCKET + '/' + thumbnailkey +
' due to an error: ' + err
));
} else {
context.succeed(
'Successfully resized ' + BUCKET + '/' + pdfkey +
' and uploaded to ' + BUCKET + '/' + thumbnailkey
);
}
}
);
Any help would be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire