我想创建一个每当有人上传到 S3 存储桶时调用的 Lambda 函数。该功能的目的是获取上传的文件,如果它是一个视频文件(mp4),那么制作一个新文件,它是最后一个文件的预览(使用 ffmpeg)。Lambda 函数是用 nodejs 编写的。我把这里的代码作为参考,但是我做错了,因为我得到一个错误,说没有为 SetStartTime 指定输入:
//dependecies
var async = require('async');
var AWS = require('aws-sdk');
var util = require('util');
var ffmpeg = require('fluent-ffmpeg');
// get reference to S3 client
var s3 = new AWS.S3();
exports.handler = function(event, context, callback) {
// Read options from the event.
console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
var srcBucket = event.Records[0].s3.bucket.name;
// Object key may have spaces or unicode non-ASCII characters.
var srcKey =
decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var dstBucket = srcBucket;
var dstKey = "preview_" + srcKey;
// Sanity check: validate that source and destination are different buckets.
if (srcBucket == dstBucket) {
callback("Source and destination buckets are the same.");
return;
}
// Infer the video type.
var typeMatch = srcKey.match(/\.([^.]*)$/);
if (!typeMatch) {
callback("Could not determine the video type.");
return;
}
var videoType = typeMatch[1];
if (videoType != "mp4") {
callback('Unsupported video type: ${videoType}');
return;
}
// Download the video from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the video from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
console.log("response.Body:\n", response.Body);
ffmpeg(response.Body)
.setStartTime('00:00:03')
.setDuration('10') //.output('public/videos/test/test.mp4')
.toBuffer(videoType, function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
}
], function (err) {
if (err) {
console.error(
'Unable to modify ' + srcBucket + '/' + srcKey +
' and upload to ' + dstBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully modify ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey
);
}
callback(null, "message");
}
);
};
那么我做错了什么?