[英]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.