将string附加到AWS Lambda中的文本文件Nodejs
场景:文本文件snapshot-ids.txt
位于S3存储桶中。 我正在尝试创build一个每日运行的Lambda函数(Cron),它将使用AWS CLI获取卷的快照,然后将该snapshotId保存到S3中的文本文件中。 在下一次创build另一个快照时,新的snapshotId将被保存到S3上的同一文本文件中。 文本文件是snapshotIds的占位符,当达到阈值时,它将删除顶部的snapshotIds并在末尾添加新的(FIFOpipe道)。
对于不使用AWS lambda的人来说,我的问题是将文本追加到variables的最快方法是什么,并用新行返回新variables。
对于了解Lambda的人,这是AWS Lambda的基本代码,我使用fs.appendFile,但是如何使用从s3.getObject()获得的文件并最终将其传递给s3.putObject()?
编辑:这是我的进步:
console.log('Loading function'); var aws = require('aws-sdk'); var s3 = new aws.S3({ apiVersion: '2006-03-01' }); var fs = require('fs'); exports.handler = function(event, context) { //console.log('Received event:', JSON.stringify(event, null, 2)); // Get the object from the event and show its content type var bucket = event.Records[0].s3.bucket.name; var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); var params = { Bucket: bucket, Key: key }; s3.getObject(params, function(err, data) { if (err) { console.log(err); var message = "Error getting object " + key + " from bucket " + bucket + ". Make sure they exist and your bucket is in the same region as this function."; console.log(message); context.fail(message); } else { // fs.appendFile('snapshot-ids.txt', 'snap-001', function (err) { // if (err) throw err; // console.log('The "data to append" was appended to file!'); // }); console.log(params_new); console.log('CONTENT TYPE getObject:', data.ContentType); // context.succeed(data.Body.toString('ascii')); } }); var params_new = { Bucket: bucket, Key: key, Body: 'snap-002' }; s3.putObject(params_new, function(err, data) { console.log('put here'); if (err) { console.log(err); var message = "Error getting object " + key + " from bucket " + bucket + ". Make sure they exist and your bucket is in the same region as this function."; console.log(message); context.fail(message); } else { console.log('CONTENT TYPE putObject:', data.ContentType); context.succeed(data.ContentType); } }); };
我注意到你的代码到目前为止的一些事情…
-
在
s3.getObject
完成之前,您不能调用s3.putObject
,并且您有s3中的文件。 -
由于您从
s3.getObject
获取data
,s3.getObject
不处理文件系统。
考虑到这些事情,我修改了你的代码(我没有尝试过,但它应该让你朝着正确的方向前进):
console.log('Loading function'); var aws = require('aws-sdk'); var s3 = new aws.S3({ apiVersion: '2006-03-01' }); exports.handler = function(event, context) { //console.log('Received event:', JSON.stringify(event, null, 2)); // Get the object from the event and show its content type var bucket = event.Records[0].s3.bucket.name; var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); var params = { Bucket: bucket, Key: key }; s3.getObject(params, function(err, data) { if (err) { console.log(err); var message = "Error getting object " + key + " from bucket " + bucket + ". Make sure they exist and your bucket is in the same region as this function."; console.log(message); context.fail(message); } else { console.log(params_new); console.log('CONTENT TYPE getObject:', data.ContentType); // convert body(file contents) to a string so we can append var body = data.Body.toString('utf-8'); // append data body += 'snap-001\n'; var params_new = { Bucket: bucket, Key: key, Body: body }; //NOTE this call is now nested in the s3.getObject call so it doesn't happen until the response comes back s3.putObject(params_new, function(err, data) { console.log('put here'); if (err) { console.log(err); var message = "Error getting object " + key + " from bucket " + bucket + ". Make sure they exist and your bucket is in the same region as this function."; console.log(message); context.fail(message); } else { console.log('CONTENT TYPE putObject:', data.ContentType); context.succeed(data.ContentType); } }); } }); };
还有一点需要注意的是,如果你有超过1个的Lambda在同一时间运行,那么他们可能会踩踏对方的变化。 听起来你会只是每天安排一次,所以它不应该是一个大问题,但它值得注意。
- 从AWS Lambda Node.JSstream式传输到S3
- Amazon AWS Lambda Alexa HTTP获取问题
- Amazon SQS是将分析日志logging数据处理到数据库的好工具吗?
- 以编程方式创buildaws API密钥并将其添加到单一function的使用计划?
- AWS Lambda函数连接到Postgresql数据库
- Node.js获取高内存使用率的通知
- 使用aws-sdk使用nodejs将图像上传到s3
- AWS Cognito Developer身份validationstream程实施服务器端
- MalformedXML:您提供的XML格式不正确,或者没有针对我们发布的模式进行validation