如何使用lambda函数从AWS s3获取文本文件的内容?

如何使用lambda函数从AWS s3获取文本文件的内容?

问题描述:

我想知道是否可以为AWS设置lambda函数,只要将新文本文件上传到s3存储桶中就会触发。在函数中,我想获取文本文件的内容并以某种方式处理它。我想知道这是否可能......?

I was wondering if I could set up a lambda function for AWS, triggered whenever a new text file is uploaded into an s3 bucket. In the function, I would like to get the contents of the text file and process it somehow. I was wondering if this was possible...?

例如,如果我上传foo.txt,内容为foobarbaz,我想以某种方式在我的lambda中获取foobarbaz功能所以我可以做它的东西。我知道我可以从getObject或类似方法获取元数据。

For example, if I upload foo.txt, with contents foobarbaz, I would like to somehow get foobarbaz in my lambda function so I can do stuff with it. I know I can get metadata from getObject, or a similar method.

谢谢!

S3对象键和存储桶名称通过事件参数传递到Lambda函数。然后,您可以从S3获取对象并读取其内容。

The S3 object key and bucket name are passed into your Lambda function via the event parameter. You can then get the object from S3 and read its contents.

从Lambda 事件中检索存储桶和对象密钥的基本代码如下:

Basic code to retrieve bucket and object key from the Lambda event is as follows:

exports.handler = function(event, context, callback) {
   var src_bkt = event.Records[0].s3.bucket.name;
   var src_key = event.Records[0].s3.object.key;
};

获得存储桶和密钥后,可以调用getObject来检索对象:

Once you have the bucket and key, you can call getObject to retrieve the object:

var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = function(event, context, callback) {

    // Retrieve the bucket & key for the uploaded S3 object that
    // caused this Lambda function to be triggered
    var src_bkt = event.Records[0].s3.bucket.name;
    var src_key = event.Records[0].s3.object.key;

    // Retrieve the object
    s3.getObject({
        Bucket: src_bkt,
        Key: src_key
    }, function(err, data) {
        if (err) {
            console.log(err, err.stack);
            callback(err);
        } else {
            console.log("Raw text:\n" + data.Body.toString('ascii'));
            callback(null, null);
        }
    });
};

许多海报都要求用Java中的等价物,所以这是一个例子:

A number of posters have asked for the equivalent in Java, so here's an example:

package example;

import java.net.URLDecoder;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;

public class S3GetTextBody implements RequestHandler<S3Event, String> {

    public String handleRequest(S3Event s3event, Context context) {
        try {
            S3EventNotificationRecord record = s3event.getRecords().get(0);

            // Retrieve the bucket & key for the uploaded S3 object that
            // caused this Lambda function to be triggered
            String bkt = record.getS3().getBucket().getName();
            String key = record.getS3().getObject().getKey().replace('+', ' ');
            key = URLDecoder.decode(key, "UTF-8");

            // Read the source file as text
            AmazonS3 s3Client = new AmazonS3Client();
            String body = s3Client.getObjectAsString(bkt, key);
            System.out.println("Body: " + body);
            return "ok";
        } catch (Exception e) {
            System.err.println("Exception: " + e);
            return "error";
        }
    }
}