我一直在尝试创建一个AWS节点lambda函数,从S3下载PDF文件,为该文件的第一页生成缩略图,并将该缩略图上传回S3。由于我不是专家,我尝试从AWS提供的Lambda示例以及在SO上找到的一个node.js中的PDF缩略图生成器来启发自己,但是我没有能够使它工作。
从S3下载工作正常,上传回S3也正常,但是生成缩略图失败。
请见下面的代码:
// 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
);
}
}
);