【发布时间】:2015-12-25 00:46:49
【问题描述】:
我一直在尝试创建一个 AWS 节点 lambda 函数来从 S3 下载 PDF 文件,为该文件的第一页生成缩略图,然后将该缩略图上传回 S3。由于我不是专家,我尝试从 AWS 提供的 Lambda 示例中启发自己来调整图像大小以及在 node.js 中的 PDF 缩略图生成器的 SO 上找到的 node.js,但未能使其工作。 从 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
);
}
}
);
任何帮助将不胜感激!
【问题讨论】:
-
你有没有想过这个问题?想做同样的事情。干杯
标签: javascript amazon-web-services pdf amazon-s3 aws-lambda