简体   繁体   English

AWS Lambda函数无法将json更新到s3存储桶

[英]AWS Lambda function to updload json to s3 bucket not working

When I run this function, it says that it succeeds, but the file is not in the s3 bucket. 当我运行此函数时,它说成功了,但是文件不在s3存储桶中。

var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {

    AWS.config.update({
        region: 'us-west-2',
        accessKeyId: 'xxx',
        secretAccessKey: 'xxx'
    });

    var s3 = new AWS.S3();

    s3.putObject({
        Bucket: 'mybucket',
        Key: 'test.json',
        Body: [{test: 'test'}, {test: 'test'}],
        ContentType: "application/json"
    }, function(){});

    context.succeed('SUCCESS');
};

NodeJs is a interesting language. NodeJs是一种有趣的语言。

Most languages would process your code like this 大多数语言都会像这样处理您的代码

  1. AWS.config.update({..}) AWS.config.update({..})
  2. var s3 = .. var s3 = ..
  3. s3.putObject(.., callback); s3.putObject(..,回调); (This takes time, so it would wait till it finishes) (这需要时间,因此将等待直到完成)
  4. context.succeed(..) context.succeed(..)

Node js will process in this order. Node js将按此顺序处理。

  1. AWS.config.update({..}) AWS.config.update({..})
  2. var s3 = .. var s3 = ..
  3. s3.putObject(.., callback); s3.putObject(..,回调); (This takes time, so I will go to next step) (这需要时间,因此我将转到下一步)
  4. context.succeed(..) context.succeed(..)
  5. the s3.putObject(.., callback) has finished. s3.putObject(..,回调)已经完成。 I'll call the callback now. 我现在将调用该回调。
  6. callback(err, res). 回调(err,res)。 It calls the callback with the error. 它使用错误调用回调。 Since your program has an empty callback, function(){}, it will not do anything with the error and you won't know about it. 由于您的程序有一个空的回调函数function(){},因此不会对该错误执行任何操作,并且您将一无所知。
 var AWS = require('aws-sdk'); exports.handler = (event, context, callback) => { AWS.config.update({ region: 'us-west-2', accessKeyId: 'xxx', secretAccessKey: 'xxx' }); var s3 = new AWS.S3(); s3.putObject( { Bucket: 'mybucket', Key: 'test.json', Body: [{test: 'test'}, {test: 'test'}], ContentType: "application/json" }, function(err, res){ if(err){ console.log(err); } callback(err, "processed"); }); }; 

^The callback will tell lambda that it is the end of the script. ^该回调将告诉lambda它是脚本的结尾。 It will now process like this. 现在将按以下方式处理。

  1. AWS.config.update({..}) AWS.config.update({..})
  2. var s3 = .. var s3 = ..
  3. s3.putObject(.., callback); s3.putObject(..,回调); (This takes time, but there isn't another step so I'll wait) (这需要时间,但是没有其他步骤,因此我将等待)
  4. the s3.putObject(.., callback) has finished. s3.putObject(..,回调)已经完成。 I'll call the callback now. 我现在将调用该回调。
  5. callback(err, res). 回调(err,res)。 It calls the callback with the error. 它使用错误调用回调。 You will print the error and be able to debug your program. 您将打印错误并能够调试程序。
  6. callback(err, 'processed') will end the lambda. callback(err,'processed')将结束lambda。

您可以使用Q https://www.npmjs.com/package/q或NodeJs promises以要求的顺序处理任务。

暂无
暂无

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

相关问题 AWS S3 Lambda事件-从同一存储桶中获取,更新然后放入JSON文件 - AWS S3 Lambda event - get, update, then put JSON file from same bucket 是否可以通过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? 如何从 s3 存储桶解析 CSV 以在 javascript AWS Lambda function 中使用 - How to parse CSVs from s3 bucket to use in a javascript AWS Lambda function 当S3存储桶中的getObject时,对AWS Lambda函数的访问被拒绝 - Access denied on aws lambda function when getObject from S3 bucket 使用 aws lambda 函数中的节点 js 在其中创建 s3 存储桶和文件夹 - creating s3 bucket and folders inside it using node js in aws lambda function 代码管道将S3存储桶作为用户参数传递给AWS中的lambda函数 - Code pipeline passing S3 bucket as User Parameters to lambda function in AWS 尝试从 Nodejs 中的 AWS Lambda 函数读取 S3 存储桶的内容时未获得结果 - Not getting results when attempting to read the content of an S3 bucket from AWS Lambda function in Nodejs 如何使用带有Lambda函数的Node.js在AWS S3存储桶中创建嵌套文件夹? - How to create nested folders in aws s3 bucket using nodejs with lambda function? 使用 lambda 扫描 AWS S3 存储桶中的文件以查找病毒 - Scan files in AWS S3 bucket for virus using lambda aws将对象上传到S3存储桶,并将数据详细信息传递给lambda - aws upload object to S3 bucket and pass details of data to lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM