简体   繁体   English

使用nodejs从lambda触发器调整S3存储桶中的图像大小

[英]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:基本上它是这样工作的:

  • Upload this code as your lambda.将此代码上传为您的 lambda。
  • Go to the triggers tab of your lambda and copy the URL转到 lambda 的触发器选项卡并复制 URL
  • Go to your s3 bucket and set up a redirection rule: on 404, redirect to the lambda URL.转到您的 s3 存储桶并设置重定向规则:在 404 上,重定向到 lambda URL。 The image will be automatically resized when requested.图像将在请求时自动调整大小。

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.

相关问题 访问被拒绝错误 500 从 Lambda function 中删除 S3 存储桶中的 Object 使用 NodeJS 和 Z48C061E29CEAED2 - Access Denied Error 500 Deleting Object in S3 Bucket from Lambda function using NodeJS and Postman 如何将图像文件从 s3 存储桶上传到 cloudinary (nodejs) - How to upload an image file from an s3 bucket to cloudinary (nodejs) NodeJS:从 AWS S3 存储桶返回图像 - NodeJS: Returning image from AWS S3 bucket NodeJS:从 AWS S3 存储桶获取图像作为响应 - NodeJS: Get image as response from AWS S3 bucket 使用 nodejs 从 AWS S3 存储桶下载图像文件,更新:12/12 - downloading image file from AWS S3 bucket using nodejs, UPDATE :12/12 使用nodejs knox从s3存储桶中删除所有文件? - Remove all files from s3 bucket using nodejs knox? 使用 Lambda 将 ndjson 写入 S3 存储桶 - Writing ndjson to S3 Bucket using Lambda 如何使用带有Lambda函数的Node.js在AWS S3存储桶中创建嵌套文件夹? - How to create nested folders in aws s3 bucket using nodejs with lambda function? 如何将数据从S3存储桶加载到Nodejs Lambda中的Elastic Search - How to load data from S3 bucket to Elastic Search in Nodejs Lambda 尝试从 Nodejs 中的 AWS Lambda 函数读取 S3 存储桶的内容时未获得结果 - Not getting results when attempting to read the content of an S3 bucket from AWS Lambda function in Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM