将字符串追加到AWS Lambda中的文本文件Nodejs [英] Append string to a text file Nodejs in AWS Lambda

查看:102
本文介绍了将字符串追加到AWS Lambda中的文本文件Nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:一个文本文件snapshot-ids.txt位于S3存储桶中.我正在尝试创建一个每天运行(Cron)的Lambda函数,该函数将使用AWS CLI拍摄卷的快照,然后将该快照ID保存到S3中的文本文件中.下次创建另一个快照时,新的snapshotId将保存到S3上的同一文本文件中.该文本文件是快照ID的占位符,当达到阈值时,它将删除顶部的快照ID,并在末尾添加新的快照ID(FIFO管道).

The scenario: A text file snapshot-ids.txt is located in a S3 bucket. I'm trying to create a Lambda function that run daily (Cron) that would use AWS CLI to take snapshot of a volume, then save that snapshotId to a text file in S3. On the next time another snapshot is created, the new snapshotId will be saved to the same text file on S3. The text file is a place holder for snapshotIds and when it reaches a threshold, it will delete the top snapshotIds and add the new one at the end (FIFO pipe).

对于不使用AWS lambda的人,我的问题是将文本追加到变量并返回带有新行的新变量的最快方法是什么.

For people who don't use AWS lambda, my question is what's the quickest way to append text to a variable and return the new variable with new lines in it.

对于了解Lambda的人来说,这是我拥有的AWS Lambda的基本代码,我使用fs.appendFile,但如何使用从s3.getObject()获得的文件并将其最终传递给s3.putObject( )?

For people who know Lambda, this is the basic code from AWS Lambda I have, I use fs.appendFile, but how do I use the file I got from s3.getObject() and eventually pass it to 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);
                }
    });
};

推荐答案

到目前为止,我在您的代码中注意到了几件事...

A couple of things I noticed with your code so far...

  1. s3.getObject完成并且您拥有s3中的文件之前,您无法调用s3.putObject.

  1. You can't call s3.putObject until s3.getObject is finished and you have the file from s3.

由于您是从s3.getObject获得data的,因此您无需处理文件系统.

You aren't dealing with the file system since you get the data from s3.getObject.

考虑到这些,我修改了您的代码(我没有尝试过,但是应该可以使您朝正确的方向前进):

With those things in mind I modified your code(I haven't tried this but it should get you going in the right direction):

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);
                        }
            });

        }
    });

};

还有一点要记住的是,如果您同时运行多个Lambda,则它们很可能会互相mp脚.听起来您每天只安排一次,所以这没什么大不了的,但是值得注意.

Something else to keep in mind is if you have more than 1 of this Lambda running at the same time it is likely they will stomp on each others changes. Sounds like you will just be scheduling it once a day so it shouldn't be a big deal but its worth noting.

这篇关于将字符串追加到AWS Lambda中的文本文件Nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆