我已阅读所有其他主题并尝试了一些答案,但我似乎无法弄清楚为什么会出现此错误。我的代码在 S3 存储桶中获取上传的图片,降低质量并将其放入第二个存储桶中。干净利落。使用小/中型图像一切正常,但如果我上传超过 2 MB(或多或少)的内容,我会在标题中收到错误。我的 Lambda 函数有 128MB 和 3 分钟超时;这是代码:
const gm = require('gm').subClass({imageMagick: true});
const AWS = require('aws-sdk');
const async = require('async');
const S3 = new AWS.S3();
exports.handler = (event, context, callback) => {
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey = event.Records[0].s3.object.key;
var dstBucket = "destinationbucket";
var dstKey = "resized-" + srcKey;
// Infer the image type.
var typeMatch = srcKey.match(/\.([^.]*)$/);
if (!typeMatch) {
callback("Could not determine the image type.");
return;
}
var imageType = typeMatch[1].toLowerCase();
if (imageType != "jpg" && imageType != "png" && imageType != "jpeg") {
callback('Unsupported image type: ${imageType}');
return;
}
async.waterfall([
function download(next) {
S3.getObject({Bucket : srcBucket, Key : srcKey}, next);
},
function transform(response, next) {
var img_quality_reduced = gm(response.Body);
img_quality_reduced.quality(75).toBuffer(function( error, buffer )
{
if( error ) { console.log( error ); return; }
next(null, response.ContentType, buffer);
}
);
},
function upload(contentType, data, next) {
S3.putObject({Bucket: dstBucket, Key: dstKey, Body: data}, next);
},
function ending(next) {
console.log('got to ending');
context.done();
}
], function (err) {
console.log(err);
context.done();
});
};
知道为什么会这样吗?我已将 async、gm 和 graphicsmagick 加载到 Lambda(作为 zip 文件)。全部通过 npm 下载