【问题标题】:How to parse an AWS S3 file from a Lambda function如何从 Lambda 函数解析 AWS S3 文件
【发布时间】:2015-12-07 11:30:13
【问题描述】:

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

我想使用由在 S3 存储桶中创建新对象触发的 Lambda 函数来读取文件并提取一些数据并将其写入放置在另一个 S3 存储桶中的文件。

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

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

如果我当前使用的 Node.js 代码添加了一个附加函数来处理文件,请参见下面的带有 ?? 的 cmets我在哪里寻求帮助。

// 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。但是我写了一个 Lambda,它做的事情与你所做的类似,但用 Python 编写。我不确定你到底在问什么。我看到有 ?? 的 cmets。您需要 Node.js 特定的帮助还是逻辑帮助?
  • 我认为 @helloV Node.js 的具体帮助,但如果您有一个 Python 示例,我总是可以使用 Python 来代替。
  • @helloV 我正在寻找一个解析 S3 对象并删除每行的第一个单词并将其复制回 S3 的 python 脚本。如果您还有脚本,请分享一下吗?

标签: node.js amazon-web-services amazon-s3 aws-lambda readline


【解决方案1】:

在 s3.getObject 的回调中

s3.getObject(params,function(err,data){}) 

如果您的文件是文本,那么您可以将文本提取为字符串

data.Body.toString("utf-8")

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多