我正在将此节点 js 脚本用作 aws lambda 脚本,以在将 pdf 上传到 s3 存储桶后立即创建 pdf 文件的缩略图。
var async = require('async');
var s3 = new AWS.S3();
var ffmpeg = require('fluent-ffmpeg');
var fs = require('fs');
var util = require('util');
var mktemp = require("mktemp");
var gm = require('gm').subClass({ imageMagick: true });
const https = require('https');
var mediaconvert = new AWS.MediaConvert({
apiVersion: '2017-08-29',
});
p1 = process.env['LAMBDA_TASK_ROOT'];
process.env['FFMPEG_PATH'] = p1 + '/ffmpeg-git-20180111-64bit-static/ffmpeg';
process.env['FFPROBE_PATH'] = p1 + '/ffmpeg-git-20180111-64bit-static/ffprobe';
var imageResolutions = Object.freeze({
thumbnail: 0,
type1: 1,
type2: 2
});
function processPdf(srcBucket, srcKey, fileType, format) {
var lastSlash = srcKey.lastIndexOf('/');
var dotIndex = srcKey.lastIndexOf('.');
var parentPath = srcKey.slice(0, lastSlash + 1);
var fileName = srcKey.slice(lastSlash + 1, dotIndex);
var dstKey = parentPath + 'scaled/' + fileName + '_thumbnail.png';
console.log('lastSlash:' + lastSlash + ' dotIndex ' + dotIndex + ' parentPath ' + parentPath + ' fileName ' + fileName);
async.waterfall([
function download(next) {
// Download the pdf from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function thumbnail(response, next) {
console.log('generating thumbnail for pdf');
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");
console.log("image is " + image);
image.size(function(err, size) {
// Transform the image buffer in memory.
this.resize(200, 200)
.toBuffer("jpeg", function(err, buffer) {
if (err) {
console.log("error buffer " + err);
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the thumbnail
console.log('uploading thumbnail');
s3.putObject({
Bucket: srcBucket,
Key: dstKey,
ACL:"public-read",
Body: data,
ContentType: "image/jpeg"
},
next);
}
], function (err) {
if (err) {
console.log(
'Unable to create thumbnail for ' + srcBucket + '/' + dstKey +
' and upload to ' + srcBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully resized ' + srcBucket + '/' + dstKey +
' and uploaded to ' + srcBucket + '/' + dstKey
);
}
}
);
}
exports.handler = function(event, context, callback) {
console.time('Total time taken');
var srcBucket = event.Records[0].s3.bucket.name;
console.log('Source Bucket: ' + srcBucket);
var format = "png";
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(
/\+/g, " "));
console.log('Source Key: ' + srcKey);
var pathComponents = srcKey.split('/');
if (srcKey.indexOf('scaled/') > -1) {
console.log('Already scaled....Skipping');
return;
}
var dstBucket = srcBucket;
var typeMatch = srcKey.match(/\.([^.]*)$/);
var imageType = typeMatch[1].toLowerCase();
if (imageType == "mp4") {
console.log('Processing video ' + srcKey);
processVideo(srcBucket, srcKey, imageType);
}
else if(imageType == "pdf"){
console.log('Processing video ' + srcKey);
processPdf(srcBucket, srcKey, imageType, format);
}else if(imageType == "png"){
var params = {
Bucket: srcBucket,
Key: srcKey
};
s3.getObjectTagging(params, function(err, data) { //get Tags for this object
if (err) console.log(err, err.stack); // an error occurred
else {
var isAlreadyProcessed = false;
var lastSlash = srcKey.lastIndexOf('/');
var parentPath = srcKey.slice(0, lastSlash + 1);
var name = srcKey.slice(lastSlash + 1, srcKey.indexOf('.' + imageType));
var tagSet = data.TagSet;
console.log('isAlreadyProcessed ' + isAlreadyProcessed);
console.log('lastSlash ' + lastSlash);
console.log('parentPath ' + parentPath);
console.log('name ' + name);
console.log('tagSet ' + tagSet);
tagSet.forEach(function(value) {
if (value['Key'] == 'processed' && value['Value'] == 'true') { //if 'processed' tag is set to 'true'
isAlreadyProcessed = true;
}
});
if (isAlreadyProcessed) {
console.log("Alreay processed.....Skipping");
return;
}
tagSet.push({
Key: "processed",
Value: "true"
})
scale(params, srcBucket, parentPath, name, 200, imageType, imageResolutions.thumbnail, tagSet);
}
});
}
else{
console.log("no valid data type");
return;
}
};
每当我将文件上传到 s3 存储桶时,我都会收到此错误错误:流产生空缓冲区。我已将内存增加到 2 GB 仍然存在问题。请帮忙,让我知道这里出了什么问题。