简体   繁体   English

为什么我无法使用我的 Lambda 函数将文件上传到 s3?

[英]Why can't I upload a file to s3 with my Lambda function?

I am creating a lambda function which I've tied to API gateway and my code doesn't seem to fire the s3.putObject event.我正在创建一个与 API 网关绑定的 lambda 函数,我的代码似乎没有触发 s3.putObject 事件。

I can console.log the AWS, s3, params variables with no problems but when trying to use the putObject function, nothing fires...the rest of my code simply seems to run.我可以在没有问题的情况下对 AWS、s3、params 变量进行 console.log,但是当尝试使用 putObject 函数时,没有任何反应……我的其余代码似乎只是在运行。

Does anyone know what I might need to do?有谁知道我可能需要做什么?

I've set a region on my s3 instance, an API version Logged out my variables Checked cloudwatch logs for changes我在我的 s3 实例上设置了一个区域,一个 API 版本注销了我的变量检查了 cloudwatch 日志的变化

exports.handler = async (event) => {


const AWS = require('aws-sdk');
const s3 = new AWS.S3({region: "us-east-1", apiVersion: '2006-03-01'});
const params = {
Bucket: bucketName,
Key: 'file.txt',
ContentType: 'text/plain',
Body: JSON.stringify(event)};

// The below doesn't seem to run or log out
s3.putObject(params).promise().then(data => {
    console.log('complete:PUT Object',data);
    })
  .catch(err => {
    console.log('failure:PUT Object', err);
  });

 return JSON.stringify(event);

    };

I expect to be able to go into my s3 bucket and see a file uploaded.我希望能够进入我的 s3 存储桶并查看上传的文件。 Instead its empty相反它是空的

You aren't waiting for the promise to return before telling lambda to return a result.在告诉 lambda 返回结果之前,您不是在等待承诺返回。 Promises are great if you have a heap of chaining, but in this simple case, a call back should be enough.如果你有一堆链接,Promises 很好,但在这种简单的情况下,回调就足够了。

Also if you are using async you need to make sure the runtime is 8.10 or above.此外,如果您使用 async,则需要确保运行时为 8.10 或更高版本。

exports.handler = async (event, context, callback) => {

const AWS = require('aws-sdk');
const s3 = new AWS.S3({region: "us-east-1", apiVersion: '2006-03-01'});
const params = {
Bucket: bucketName,
Key: 'file.txt',
ContentType: 'text/plain',
Body: JSON.stringify(event)};

console.log(JSON.stringify(event));

// The below doesn't seem to run or log out
s3.upload(params, options, function(err, data) {
  console.log(err, data);
  if(!err) { 
    callback(null, "All Good");
   }
  else {
    callback(err);
  }
 });
};

A little more reading on the context object.多读一点上下文对象。 https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Because you're using exports.handler = async (event) in your code, you're using async/await ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function ).因为您在代码中使用了exports.handler = async (event) ,所以您使用的是 async/await ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function )。

Within an async function you can use the await syntax to make a promise to a synchronous call:在 async 函数中,您可以使用await语法对同步调用做出承诺:

try {
  const res = await s3.upload(params, options).promise();
  console.log('complete:', res);
} catch(err) {
  console.log('error:', err);
}

This is a modern approach to callbacks and it's consistent (no mixture of callbacks and async functions).这是一种现代的回调方法,并且是一致的(没有回调和异步函数的混合)。

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

相关问题 无法上传到我的 Lambda function 内的 AWS S3 - Cannot upload to AWS S3 inside my Lambda function 无法将文件从 LAMBDA 上传到 S3 - Can't upload files from LAMBDA to S3 无法在Amazon S3上上传文件 - Can't upload file on Amazon S3 我的Lambda在我的S3上传中超时 - My Lambda is timeout with my S3 upload 为什么我无法将S3设置为无服务器Lambda函数的触发器? - Why am I unable to set S3 as a trigger for my Serverless Lambda Function? 如何通过 AWS lambda nodejs function 索引存储在 S3 中的 XML 文件? - How can I index a XML file stored in S3 through AWS lambda nodejs function? 如何使用 Node 从 SAM Lambda function 在 S3 中写入 json 文件 - How can I write a json file in S3 from a SAM Lambda function with Node 为什么我可以通过 Postman 将图像上传到我的 S3 存储桶,但不能通过 Node.js 上传图像? - Why can I upload an image to my S3 bucket through Postman, but not through Node.js? S3 putObject 没有使用 nodejs 将我的文件从 EFS 上传到 S3 Lambda - S3 putObject isn't uploading my file to S3 from EFS using nodejs 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM