[英]Resizing the image in S3 bucket from lambda trigger using nodejs
I newbie to nodejs and aws, Can anyone point out whats wrong with the following code to resize the images in s3 bucket我是 nodejs 和 aws 的新手,谁能指出以下代码有什么问题来调整 s3 存储桶中的图像大小
Program as follows程序如下
'use strict';
const AWS = require('aws-sdk');
const S3 = new AWS.S3({
accessKeyId: "xxxxxxxxxxxx",
secretAccessKey: "yyyyyyyyyyy",
region: "us-east-1",
signatureVersion: 'v4',
});
const Sharp = require('sharp');
const BUCKET = "patientimg";
const URL = "https://s3.ap-south-1.amazonaws.com";
exports.handler = function(event, context, callback) {
const key = event.queryStringParameters.key;
const match = key.match(/(\d+)x(\d+)\/(.*)/);
const width = parseInt(match[1], 10);
const height = parseInt(match[2], 10);
const originalKey = match[3];
S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
.resize(width, height)
.toFormat('png')
.toBuffer()
)
.then(buffer => S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: "image/png",
Key: key,
}).promise()
)
.then(() => callback(null, {
statusCode: '301',
headers: {'location': "${URL}/${key}"},
body: "",
})
)
.catch(err => callback(err))
}
this is my exact code I'm using, output from lambda when testing with "S3 put" request这是我正在使用的确切代码,使用“S3 put”请求进行测试时从 lambda 输出
{
"errorMessage": "RequestId: edaddaf7-4c5e-11e7-bed8-13f72aaa5d38 Process exited before completing request"
}
Thanks in advance提前致谢
Resizing images using a lambda is a classic example that has been well explained by the AWS team.使用 lambda 调整图像大小是一个经典示例,AWS 团队对此进行了很好的解释。 Follow their instructions, not something else.
听从他们的指示,而不是别的。
https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/ https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
The correct resizing code is: http://github.com/awslabs/serverless-image-resizing .正确的调整大小代码是: http : //github.com/awslabs/serverless-image-resizing 。 Whatever you found is probably wrong.
无论您发现什么,都可能是错误的。
Basically it works like this:基本上它是这样工作的:
All of these steps are well documented in detail at the AWS blog above.所有这些步骤都在上面的 AWS 博客中有详细记录。 The benefit of their approach is that the resized image is not created until it is actually needed, which saves on resources.
他们的方法的好处是在实际需要之前不会创建调整大小的图像,从而节省资源。
You can use thisAWS Lambda image resizer .您可以使用此AWS Lambda 图像调整器。
It's built with Node.js and with options to build your own settings.它是使用 Node.js 构建的,并带有构建您自己的设置的选项。 You just need to follow the steps here .
您只需要按照此处的步骤操作即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.