最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在二进制格式从AWS LAMBDA经由API网关上传图片到S3

IT培训 admin 4浏览 0评论

在二进制格式从AWS LAMBDA经由API网关上传图片到S3

我的LAMBDA是从我在请求用户的身体(event.body)接收图像的二进制数据。

我尝试它,没有错误上传到S3,但是当我下载的图像已损坏/不能打开。

我还需要上传的图片的URL返回给用户。

请帮忙!

module.exports.uploadImage = (event, context, callback) => {
  var buf = new Buffer(new Buffer(event.body).toString('base64').replace(/^data:image\/\w+;base64,/, ""),'base64');
  var data = {
    Key: Date.now()+"", 
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'image/png',
    ACL: 'public-read'
  };
  s3Bucket.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
      callback(null,data);
  });
};
回答如下:

您可以将图片上传到S3作为节点缓冲区。该SDK为你做转换。

const AWS = require("aws-sdk");
var s3 = new AWS.S3();

module.exports.handler = (event, context, callback) => {
  var buf = Buffer.from(event.body.replace(/^data:image\/\w+;base64,/, ""),"base64");
  var data = {
    Bucket: "sample-bucket", 
    Key: Date.now()+"", 
    Body: buf,
    ContentType: 'image/png',
    ACL: 'public-read'
  };
  s3.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
      callback(null,data);
  });
};

在二进制格式从AWS LAMBDA经由API网关上传图片到S3

我的LAMBDA是从我在请求用户的身体(event.body)接收图像的二进制数据。

我尝试它,没有错误上传到S3,但是当我下载的图像已损坏/不能打开。

我还需要上传的图片的URL返回给用户。

请帮忙!

module.exports.uploadImage = (event, context, callback) => {
  var buf = new Buffer(new Buffer(event.body).toString('base64').replace(/^data:image\/\w+;base64,/, ""),'base64');
  var data = {
    Key: Date.now()+"", 
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'image/png',
    ACL: 'public-read'
  };
  s3Bucket.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
      callback(null,data);
  });
};
回答如下:

您可以将图片上传到S3作为节点缓冲区。该SDK为你做转换。

const AWS = require("aws-sdk");
var s3 = new AWS.S3();

module.exports.handler = (event, context, callback) => {
  var buf = Buffer.from(event.body.replace(/^data:image\/\w+;base64,/, ""),"base64");
  var data = {
    Bucket: "sample-bucket", 
    Key: Date.now()+"", 
    Body: buf,
    ContentType: 'image/png',
    ACL: 'public-read'
  };
  s3.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
      callback(null,data);
  });
};
发布评论

评论列表 (0)

  1. 暂无评论