如何从Lambda函数解析AWS S3文件

4

我需要一些帮助来正确地构建代码,使用S3存储桶和Lambda函数处理一些文本文件。

我想使用一个Lambda函数,当在S3存储桶中创建新对象时触发该函数,以读取文件并提取某些数据,然后将这些数据写入到放置在另一个S3存储桶中的文件中。

到目前为止,我的函数已经可以很好地从一个S3存储桶复制文件到另一个S3存储桶,但我还不太清楚如何添加一个函数来处理文件并将结果写入最终的S3目标。

这些文件是简单的文本文件,我需要从每行中提取数据。

以下是我目前正在使用的Node.js代码,其中添加了一个附加函数来处理文件 - 请参见带有“?”的注释,那里是我需要帮助的地方。

// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var util = require('util');


// get reference to S3 client 
var s3 = new AWS.S3();

exports.handler = function(event, context) {
    // Read options from the event.
    console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
    var srcBucket = event.Records[0].s3.bucket.name;
    // Object key may have spaces or unicode non-ASCII characters.
    var srcKey    =
    decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));  
    var dstBucket = "inputBucket";
    var dstKey    = srcKey + ".txt";

    // Sanity check: validate that source and destination are different buckets.
    if (srcBucket == dstBucket) {
        console.error("Destination bucket must not match source bucket.");
        return;
    }

    // Infer the file type.
    var typeMatch = srcKey.match(/\.([^.]*)$/);
    if (!typeMatch) {
        console.error('unable to infer file type for key ' + srcKey);
        return;
    }
    var imageType = typeMatch[1];
    if (imageType != "txt") {
        console.log('skipping non-image ' + srcKey);
        return;
    }

    // Download the image from S3, transform, and upload to a different S3 bucket.
    async.waterfall([
        function download(next) {
            // Download the file from S3 into a buffer.
            s3.getObject({
                    Bucket: srcBucket,
                    Key: srcKey
                },
                next);
            },
        function transform(response, next) {
            // Read the file we have just downloaded 
            // ? response.Body ?
            var rl = require('readline').createInterface({
                input: require('fs').createReadStream('file.in')
            });

            // Process each line here writing the result to an output buffer?
            rl.on('line', function (line) {
                 console.log('Line from file:', line);
                //Do something with the line... 

                //Create some output string 'outputline'

                //Write 'outputline' to an output buffer 'outbuff'
                // ??

            });
            // Now pass the output buffer to the next function
            // so it can be uploaded to another S3 bucket 
            // ?? 
            next;
        }
        function upload(response, next) {
            // Stream the file to a different S3 bucket.
            s3.putObject({
                    Bucket: dstBucket,
                    Key: dstKey,
                    Body: response.Body,
                    ContentType: response.contentType
                },
                next);
            }
        ], function (err) {
            if (err) {
                console.error(
                    'Unable to process ' + srcBucket + '/' + srcKey +
                    ' and upload to ' + dstBucket + '/' + dstKey +
                    ' due to an error: ' + err
                );
            } else {
                console.log(
                    'Successfully processed ' + srcBucket + '/' + srcKey +
                    ' and uploaded to ' + dstBucket + '/' + dstKey
                );
            }

            context.done();
        }
    );
};

我不懂Node.js。但是我已经用Python编写了一个类似于你所做的Lambda。我不确定你具体在问什么。我看到了带有??的评论。你需要Node.js特定的帮助还是逻辑上的帮助? - helloV
@helloV 我认为这是关于Node.js的特定帮助,但如果您有一个使用Python完成相同任务的示例,我也可以使用Python。 - Duncan Groenewald
@helloV 我正在寻找一段解析S3对象并删除每行第一个单词并将其复制回S3的Python脚本。如果您仍然有这个脚本,能否分享一下? - user5556585
1个回答

1
在s3.getObject的回调函数中。
s3.getObject(params,function(err,data){}) 

如果您的文件是文本,则可以将其作为字符串提取。
data.Body.toString("utf-8")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接