2015-06-04 64 views
18

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

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

谢谢!

回答

20

是的,你可以做到这一点。如果您阅读Lambda programming docs,您将看到S3对象密钥(和存储桶名称)通过参数事件传递到您的Lambda函数。然后,您可以从S3获取对象,阅读其内容,然后执行您需要的任何操作。 BTW对象的内容不是元数据;它是数据。

原来的例子是通过PDF在wayback machine现已推出,但基本的代码从拉姆达event检索桶和对象主要内容如下:

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检索对象,这里是一个更完整的示例,说明如何做到这一点:

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中的等价物,所以这里有一个例子:

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"; 
     } 
    } 
} 
+0

对,但除非我错了,不是'console.log('CONTENT TYPE:',data.ContentType);'元数据而不是文件的内容中的数据? – jstnchng

+0

它给你的事件数据,但不是文件本身的数据,iirc。 – jstnchng

+0

@jstnchng是的,这是元数据。但我认为你要求的是'foobarbaz'这是实际的S3对象的内容,所以你必须调用GetObject来检索对象。 – jarmod

12

您可以使用data.Body.toString('ascii')来获取文本文件的内容,假定文本文件使用ascii格式编码。您也可以将其他编码类型传递给该函数。查看Node-Buffer了解更多详情。

+0

工程就像一个魅力,顺便说一句,你可以看看我的类似问题? http://stackoverflow.com/questions/34056133/append-string-to-a-text-file-nodejs-in-aws-lambda – Casper

+0

这应该是公认的答案imho –

相关问题