Amazon s3 无法使用无服务器框架将映像上载到s3,但它可以脱机工作(缓冲区问题)

Amazon s3 无法使用无服务器框架将映像上载到s3,但它可以脱机工作(缓冲区问题),amazon-s3,aws-lambda,aws-api-gateway,serverless-framework,image-upload,Amazon S3,Aws Lambda,Aws Api Gateway,Serverless Framework,Image Upload,我正在尝试部署一个lambda函数,允许我将图片上传到S3。 lambda在离线状态下工作得很好,但是当我将其部署到AWS时,该函数就不起作用了 我遇到的第一个错误是: ERROR (node:7) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or B

我正在尝试部署一个lambda函数,允许我将图片上传到S3。 lambda在离线状态下工作得很好,但是当我将其部署到AWS时,该函数就不起作用了

我遇到的第一个错误是:

ERROR   (node:7) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
因此,我按照建议使用Buffer.from()方法。但它也不起作用。lambda将一直运行到超时

有人能告诉我哪里错了,或者给我另一个解决方案吗

下面是我的lambda函数:

const AWS = require("aws-sdk");
const Busboy = require("busboy");
const uuidv4 = require("uuid/v4");
require("dotenv").config();

AWS.config.update({
  accessKeyId: process.env.ACCESS_KEY_ID,
  secretAccessKey: process.env.SECRET_ACCESS_KEY,
  subregion: process.env.SUB_REGION
});

const s3 = new AWS.S3();

const getContentType = event => {
  // see the second block of codes
};

const parser = event => {
  // see the third block of codes
};


module.exports.main = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  const uuid = uuidv4();

  const uploadFile = async (image, uuid) =>
    new Promise(() => {
      // const bitmap = new Buffer(image, "base64"); // <====== deprecated
      const bitmap = Buffer.from(image, "base64"); // <======== problem here
      const params = {
        Bucket: "my_bucket",
        Key: `${uuid}.jpeg`,
        ACL: "public-read",
        Body: bitmap,
        ContentType: "image/jpeg"
      };
      s3.putObject(params, function(err, data) {
        if (err) {
          return callback(null, "ERROR");
        }
        return callback(null, "SUCCESS");
      });
    });

  parser(event).then(() => {
    uploadFile(event.body.file, uuid);
  });
};
解析器()


您使用的是
callbackaitsforeptyeventloop
,它基本上让lambda函数认为工作还没有结束。而且,你是在用承诺来包装它,而不是解决它。您可以使用
aws sdk

module.exports.main = async event => {
  const uuid = uuidv4();
  await parser(event); // not sure if this needs to be async or not. check
  const bitmap = Buffer.from(event.body.file, "base64"); // <======== problem here
  const params = {
    Bucket: "my_bucket",
    Key: `${uuid}.jpeg`,
    ACL: "public-read",
    Body: bitmap,
    ContentType: "image/jpeg"
  };
  const response = await s3.putObject(params).promise();

  return response;
};
module.exports.main=异步事件=>{
常量uuid=uuidv4();
等待解析器(事件);//不确定是否需要异步。检查

常量位图=Buffer.from(event.body.file,“base64”);//您好,我删除了
context.callbackhaitsforemptyeventloop=false;
但它仍然不起作用是的,我尝试了您的建议,但lambda一直运行到超时,CloudWatch中没有任何错误。我忘了在s3调用前放置wait。更新了我的回答不幸的是,没有任何更改:(我用全部代码更新了我的帖子。谢谢你的帮助。谢谢你拉格哈文德拉,但这就是我做的。你检查过了吗?你解决了这个问题吗?我也遇到了同样的问题。
const parser = event =>
  new Promise((resolve, reject) => {
    const busboy = new Busboy({
      headers: {
        "content-type": getContentType(event)
      }
    });

    const result = {};

    busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
      file.on("data", data => {
        result.file = data;
      });

      file.on("end", () => {
        result.filename = filename;
        result.contentType = mimetype;
      });
    });

    busboy.on("field", (fieldname, value) => {
      result[fieldname] = value;
    });

    busboy.on("error", error => reject(error));
    busboy.on("finish", () => {
      event.body = result;
      resolve(event);
    });

    busboy.write(event.body, event.isBase64Encoded ? "base64" : "binary");
    busboy.end();
  });
module.exports.main = async event => {
  const uuid = uuidv4();
  await parser(event); // not sure if this needs to be async or not. check
  const bitmap = Buffer.from(event.body.file, "base64"); // <======== problem here
  const params = {
    Bucket: "my_bucket",
    Key: `${uuid}.jpeg`,
    ACL: "public-read",
    Body: bitmap,
    ContentType: "image/jpeg"
  };
  const response = await s3.putObject(params).promise();

  return response;
};
new Buffer(number)            // Old
Buffer.alloc(number)          // New

new Buffer(string)            // Old
Buffer.from(string)           // New

new Buffer(string, encoding)  // Old
Buffer.from(string, encoding) // New

new Buffer(...arguments)      // Old
Buffer.from(...arguments)     // New