将字符串附加到 AWS Lambda 中的文本文件 Nodejs

Append string to a text file Nodejs in AWS Lambda

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

对于不使用 AWS lambda 的人,我的问题是将文本附加到变量和 return 包含新行的新变量的最快方法是什么。

对于了解 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);
                }
    });
};

到目前为止,我在您的代码中注意到了一些事情...

  1. s3.getObject 完成并且您拥有来自 s3 的文件之前,您不能调用 s3.putObject

  2. 您没有处理文件系统,因为您从 s3.getObject 获得了 data

考虑到这些因素,我修改了您的代码(我还没有尝试过,但它应该能让您朝着正确的方向前进):

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 运行,它们很可能会相互干扰。听起来你每天只安排一次,所以这应该没什么大不了的,但值得注意。