javascript – 创建lambda函数以生成PDF文件缩略图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 创建lambda函数以生成PDF文件缩略图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4370字,纯文字阅读大概需要7分钟。
内容图文
我一直在尝试创建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
);
}
}
);
任何帮助将不胜感激!
解决方法:
这是最终工作的代码.下面的代码在节点模块中有以下4个库:
>异步
> gm
> mktemp
> pdf-image
:
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true }); // Enable ImageMagick integration.
var util = require('util');
var fs = require('fs');
var mktemp = require("mktemp");
var BUCKET = "XXXXXXXXXX";
var s3 = new AWS.S3();
exports.handler = function(event, context) {
var pdfkey = decodeURIComponent(event.pdfkey.replace(/\+/g, " "));
var thumbnailkey = decodeURIComponent(event.thumbnailkey.replace(/\+/g, " "));
var requestedwidth = event.width;
var requestedheight = event.height;
var shape = event.shape;
var format = event.format;
// Infer the pdf type.
var typeMatch = pdfkey.match(/\.([^.]*)$/);
if (!typeMatch) {
context.fail(new Error('unable to infer pdf type for key ' + pdfkey));
return;
}
var fileType = typeMatch[1];
if (fileType != "pdf") {
context.fail(new Error('skipping non-pdf ' + pdfkey));
return;
}
// 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) {
console.log('generating thumbnail');
var temp_file, image;
temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.pdf");
fs.writeFileSync(temp_file, response.Body);
image = gm(temp_file + "[0]").flatten().colorspace("CMYK");
image.size(function(err, size) {
if ((requestedwidth > 0) && (requestedheight > 0))
{
if (shape == "pad")
{
// Transform the image buffer in memory.
this.resize(requestedwidth, requestedheight).gravity('Center').background('transparent').extent(requestedwidth, requestedheight)
.toBuffer(format.toUpperCase(), function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
}
else
{
// Transform the image buffer in memory.
this.resize(requestedwidth, requestedheight)
.toBuffer(format.toUpperCase(), function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
}
}
else
{
if (requestedwidth > 0)
{
// Transform the image buffer in memory.
this.resize(requestedwidth)
.toBuffer(format.toUpperCase(), function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
}
else
{
// Transform the image buffer in memory.
this.resize(null, requestedheight)
.toBuffer(format.toUpperCase(), function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
}
}
});
},
function upload(contentType, data, next) {
// Stream the thumbnail
console.log('uploading thumbnail');
s3.putObject({
Bucket: BUCKET,
Key: thumbnailkey,
ACL:"public-read",
Body: data,
ContentType: "image/" + format
},
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 – 创建lambda函数以生成PDF文件缩略图全部内容,希望文章能够帮你解决javascript – 创建lambda函数以生成PDF文件缩略图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。