简体   繁体   English

使用 Lambda 和 API 网关上传到 S3 后文件损坏

[英]File corrupted after upload to S3 using Lambda and API Gateway

I am trying upload the file using Lambda to S3 way API GateWay, i am using the code below, the file is sent to S3 such as show the images 'test_using_postman.png' and 's3_file.png', however when I try open, the show message the file corrupted such as show the 'open_file_with_problem.png'.我正在尝试使用 Lambda 将文件上传到 S3 方式 API GateWay,我正在使用下面的代码,文件被发送到 S3,例如显示图像“test_using_postman.png”和“s3_file.png”,但是当我尝试打开时,显示文件损坏的消息,例如显示“open_file_with_problem.png”。 The images are below.图片如下。

Somone know the happened?有人知道发生了什么吗?

Thank you!谢谢!

Images:图片:

Test Using Postman: https://drive.google.com/open?id=1eenEnvuMQU28iI_Ltqzpw9OlCvIcY5Fg使用 Postman 进行测试: https://drive.google.com/open?id=1eenEnvuMQU28iI_Ltqzpw9OlCvIcY5Fg

S3 File: https://drive.google.com/open?id=1b1_CmIhzfc8mQj_rwCK6Xy30gzoP6HcK S3 文件: https://drive.google.com/open?id=1b1_CmIhzfc8mQj_rwCK6Xy30gzoP6HcK

Open File with problem: https://drive.google.com/open?id=1o54rLB9wWF1KxdUOkq3xAGVET7UWoqgf打开有问题的文件: https://drive.google.com/open?id=1o54rLB9wWF1KxdUOkq3xAGVET7UWoqgf

Code NodeJS:代码 NodeJS:

const crypto = require('crypto');

var AWS = require('aws-sdk');

AWS.config.update({region: 'us-east-1'});

module.exports.arquivo_upload =  (event, context, callback) => {

  let BUCKET_NAME = 'XXXXX';

  let fileContent = event.body;
  let filePath = 'upload/';
  let fileName = crypto.createHash('md5').update('niby_'+Date.now()).digest("hex");

  s3 = new AWS.S3({apiVersion: '2006-03-01'});

  var uploadParams = {
      Bucket: BUCKET_NAME, 
      Key: filePath+fileName+'.png', 
      Body: fileContent,
      ContentType: "image/png"
    };

  s3.upload(uploadParams, function (err, data) {
    if (err) {

      console.error(err);

      callback(null,{
        statusCode:400,
        body: JSON.stringify(err),
      });

    } if (data) {

      //TODO: Call other api to save file name

      console.info(data.Location);

      callback(null,{
        statusCode:200,
        body: JSON.stringify(data.Location),
      });
    }
  });
}

I resolved this problem: I sending file using base64 to API Gateway and lambda functions setup parameter "ContentEncoding. 'base64'".我解决了这个问题:我使用 base64 将文件发送到 API 网关和 lambda 函数设置参数“ContentEncoding.'base64'”。

  var uploadParams = {
  Bucket: config.s3.bucket_name, 
  Key: config.s3.file_path+fileName+obj.extension, 
  Body: buf,
  ContentEncoding: 'base64',
  ContentType: obj.content_type,
  ACL: "public-read"
};

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

相关问题 通过 AWS Gateway Api 通过 Lambda 将文件上传到 S3 - Upload file via AWS Gateway Api through Lambda to S3 从 java 服务器上传到 S3 存储桶的多部分文件被损坏(即使在 API 网关中启用多部分/表单数据) - Multipart file upload from java server to S3 bucket gets corrupted(Even after enable multipart/form-data in API Gateway) 如何使用API​​网关和Lambda将CSV文件上传到AWS S3 - How to upload a csv file to AWS S3 using API gateway and lambda 使用 HTML POST -> API-> Lambda -> s3 - 亚马逊 API 网关 p26 上传文件 - Upload files using HTML POST -> API-> Lambda -> s3 - Amazon API Gateway p26 如何使用 lambda 和 API 网关将图像上传到 s3 - How to upload image to s3 with lambda and API gateway 通过API网关上传带有S3的nodejs Lambda - nodejs Lambda with S3 upload via API Gateway 通过API网关或Lambda上传AWS S3 Muitipart - AWS S3 Muitipart Upload via API Gateway or Lambda 是否可以通过AWS API Gateway和lambda函数将PDF文件上传到S3存储桶? - Is it possible to upload a PDF file through AWS API Gateway and a lambda function to a S3 bucket? 使用 lambda 将文件上传到 amazon s3 - To upload a file to amazon s3 using lambda 使用无服务器框架上传时文件在 S3 上损坏 - File getting corrupted on S3 when upload using serverless framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM