admin管理员组

文章数量:815041

无法从lambda的s3中读取文件并在主处理程序函数nodejs中使用它

我正在尝试使用node js在aws lambda中构建json模式验证器应用程序,此处数据以json的形式存储在s3中。Schema也存储在s3中。我无法从s3获取json数据并将其用于验证。这是处理程序函数的代码。

exports.handler =async (event) =>{
   async function readfile(){ new AWS.S3().getObject(params, function(err, json_data)
    {
      if (!err) {
        const json = JSON.parse(new Buffer(json_data.Body).toString("utf8"));
console.log(json);
return json;
     }
   });
   }
   readfile();
   console.log(readfile());
   console.log(readfile().json);
   const validate = ajvpile(userSchema);
    const valid = validate(readfile());
if (valid) {
  console.log('User data is valid');
   const response = {
        statusCode: 200,
        body: JSON.stringify('User Data is valid!'),
    };
    return response;
} else {
  console.log('User data is INVALID!');
  console.log(validate.errors);
  const response = {
        statusCode: 400,
        body: JSON.stringify('User Data is Invalid!')+JSON.stringify(validate.errors),
    };
 }


回答如下:

通过在try / catch块中包含S3客户端代码,使用等待来实现代码的更简洁的方法:

    try{
     const s3File = await new AWS.S3().getObject(params).promise();
     //process your file here
    }catch(e){
     console.log(e)
    }

本文标签: 无法从lambda的s3中读取文件并在主处理程序函数nodejs中使用它