简体   繁体   English

使用AWS api网关将图像文件上传到AWS Lambda

[英]Image file upload to aws lambda using AWS api gateway

I am trying to create a lambda function which accepts an image as multipart/form-data, do some processing on the image and upload it to s3 and also returns reponse to the client. 我正在尝试创建一个lambda函数,该函数接受图像作为multipart / form-data,对图像进行一些处理,然后将其上传到s3,并将响应返回给客户端。 But i am stuck at the very first part of uploading the image to aws lambda using API gateway. 但是我停留在使用API​​网关将图像上传到AWS Lambda的第一部分。 I tried to do this in NodeJS as shown below: 我尝试在NodeJS中执行此操作,如下所示:

exports.handler = async (event, context, callback) => {
  var buf = Buffer.from(event.body.replace(/^data:image\/\w+;base64,/, ""),"base64");

  var data = {
    Bucket: "bucket-name", 
    Key: "abc.jpg", 
    Body: buf,
    ContentType: 'image/jpg',
    ACL: 'public-read'
  };
  data = await s3.upload(data).promise();
  return {
         statusCode: 200,
         body: JSON.stringify(buf),
     };

I am getting the following response in Postman by making POST request to the api: 通过向api发出POST请求,我在Postman中得到以下响应:

{
    "ETag": "\"b0e5b18d38904f109e0aef0b29e132be\"",
    "Location": "https://bucket-name.s3.us-east-2.amazonaws.com/abc.jpg",
    "key": "abc.jpg",
    "Key": "abc.jpg",
    "Bucket": "bucket-name"
}

But when i try to view the uploaded image in my browser using public url in the returned in the above response, i am getting empty image. 但是,当我尝试使用上述响应中返回的公共URL在浏览器中查看上载的图像时,我得到的图像为空。

Can somebody point me the mistake here or suggest some better approach. 有人可以在这里指出我的错误还是建议一些更好的方法。 Thank you. 谢谢。

As suggested by Ashan, you can go with best practice of uploading images via browser. 正如Ashan所建议的,您可以采用通过浏览器上传图像的最佳做法。

If the image size is not that large, here is working example of uploading image to S3 via ApiGateway/Lambda Proxy Integration. 如果图像大小不那么大, 是通过ApiGateway / Lambda代理集成将图像上传到S3的工作示例。

Lambda Function Code - Lambda功能代码-

exports.handler = (event, context, callback) => {
  let encodedImage = JSON.parse(event.body).user_avatar;
  let decodedImage = Buffer.from(encodedImage, 'base64');
  var filePath = "avatars/" + event.queryStringParameters.username + ".png"

  var params = {
    "Body": decodedImage,
    "Bucket": "bucketName",
    "Key": filePath,
    "ContentType " : "mime/png"
  };
  s3.upload(params, function (err, data) {
    if (err) {
      callback(err, null);
    } else {
      let response = {
        "statusCode": 200,
        "body": JSON.stringify(data),
        "isBase64Encoded": false
      };
      callback(null, response);
    }
  });
};

Serverless.yml Serverless.yml

service: aws-api-lambda-s3-image-upload

frameworkVersion: ">=1.1.0 <2.0.0"

provider:
  name: aws
  runtime: nodejs8.10
  iamRoleStatements:
    -  Effect: "Allow"
       Action:
         - "s3:ListBucket"
       Resource: "arn:aws:s3:::bucketName"
    -  Effect: "Allow"
       Action:
         - "s3:PutObject"
       Resource: "arn:aws:s3:::bucketName/*"

functions:
  index:
    handler: handler.handler
    events:
      - http: POST handler

Json Payload Should be - Json有效负载应为-

{
  "user_avatar" : "<<base64 encoded image>>"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM