【发布时间】:2020-07-18 18:10:37
【问题描述】:
我正在调用一个 lambda 函数来压缩上传到存储桶的图像,然后将压缩后的图像写入另一个存储桶
代码执行良好,它获取图像,但由于某种原因,它无法将压缩图像写入新存储桶
这是我的代码:
//Import compress module
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const sharp = require('sharp');
const AWS = require('aws-sdk')
const s3 = new AWS.S3()
exports.handler = async (event) =>{
// TODO implement
const srcBucket = event.Records[0].s3.bucket.name;
const srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
const dstBucket = "safarni";
const dstKey = srcKey;
console.log(srcKey, dstBucket, dstKey)
// Download the image from the S3 source bucket.
try {
const params = {
Bucket: srcBucket,
Key: srcKey
};
var origimage = await s3.getObject(params).promise();
} catch (error) {
console.log('here');
return;
}
const jpgBuffer = await sharp(origimage.Body).jpeg().toBuffer()
//Compressing the photo
const compressedjpgBuffer = await imagemin.buffer(jpgBuffer, {
plugins: [imageminMozjpeg({ quality: 85 })]
})
console.log(compressedjpgBuffer)
// Upload the thumbnail image to the destination bucket
try {
const destparams = {
Bucket: dstBucket,
Key: dstKey,
Body: compressedjpgBuffer,
};
const putResult = await s3.putObject(destparams).promise();
} catch (error) {
console.log(error);
return;
}
console.log('Successfully resized ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
【问题讨论】:
-
你的函数超时了吗,增加超时是否允许它完成?
标签: node.js amazon-web-services amazon-s3 aws-lambda