简体   繁体   English

使用Java中的Lambda函数的AWS S3事件通知

[英]AWS S3 Event notification using Lambda function in Java

I am trying to use Lambda function for S3 Put event notification. 我正在尝试使用Lambda函数进行S3 Put事件通知。 My Lambda function should be called once I put/add any new JSON file in my S3 bucket. 一旦我在S3存储桶中添加/添加任何新的JSON文件,就应该调用我的Lambda函数。 The challenge I have is there are not enough documents for this to implement such Lambda function in Java. 我遇到的挑战是没有足够的文档来实现Java中的这种Lambda函数。 Most of doc I found are for Node.js 我发现的大部分文档都是针对Node.js的

I want, my Lambda function should be called and then inside that Lambda function, I want to consume that added json and then send that JSON to AWS ES Service. 我想要,我的Lambda函数应该被调用,然后在Lambda函数中,我想使用添加的json,然后将该JSON发送到AWS ES服务。

But what all classes I should use for this? 但是我应该为此使用哪些类? Anyone has any idea about this? 有人对此有任何想法吗? S3 abd ES are all setup and running. S3 abd ES全部设置并运行。 The auto generated code for lambda is ` lambda的自动生成代码是`

@Override
public Object handleRequest(S3Event input, Context context) {
    context.getLogger().log("Input: " + input);

    // TODO: implement your handler
    return null;
}

What next?? 接下来是什么??

Handling S3 events in Lambda can be done, but you have to keep in mind, the the S3Event object only transports the reference to the object and not the object itself. 可以在Lambda中处理S3事件,但必须记住,S3Event对象只传输对象的引用而不是对象本身。 To get to the actual object you have to invoke the AWS SDK yourself. 要获取实际对象,您必须自己调用AWS SDK。 Requesting a S3 Object within a lambda function would look like this: 在lambda函数中请求S3对象如下所示:

public Object handleRequest(S3Event input, Context context) {
    AmazonS3Client s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());        

    for (S3EventNotificationRecord record : input.getRecords()) {
        String s3Key = record.getS3().getObject().getKey();
        String s3Bucket = record.getS3().getBucket().getName();
        context.getLogger().log("found id: " + s3Bucket+" "+s3Key);
        // retrieve s3 object
        S3Object object = s3Client.getObject(new GetObjectRequest(s3Bucket, s3Key));
        InputStream objectData = object.getObjectContent();
        //insert object into elasticsearch
    }        
    return null;
}

Now the rather difficult part to insert this object into ElasticSearch. 现在是将此对象插入ElasticSearch的相当困难的部分。 Sadly the AWS SDK does not provide any functions for this. 遗憾的是,AWS SDK没有为此提供任何功能。 The default approach would be to do a REST call against the AWS ES endpoint. 默认方法是对AWS ES端点执行REST调用。 There are various samples out their on how to proceed with calling an ElasticSearch instance. 有关如何继续调用ElasticSearch实例的各种示例。

Some people seem to go with the following project: 有些人似乎选择了以下项目:

Jest - Elasticsearch Java Rest Client Jest - Elasticsearch Java Rest Client

Finally, here are the steps for S3 --> Lambda --> ES integration using Java. 最后,以下是使用Java进行S3 - > Lambda - > ES集成的步骤。

  1. Have your S3, Lamba and ES created on AWS. 在AWS上创建S3,Lamba和ES。 Steps are here . 步骤在这里
  2. Use below Java code in your lambda function to fetch a newly added object in S3 and send it to ES service. 在lambda函数中使用以下Java代码在S3中获取新添加的对象并将其发送到ES服务。

     public Object handleRequest(S3Event input, Context context) { AmazonS3Client s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain()); for (S3EventNotificationRecord record : input.getRecords()) { String s3Key = record.getS3().getObject().getKey(); String s3Bucket = record.getS3().getBucket().getName(); context.getLogger().log("found id: " + s3Bucket+" "+s3Key); // retrieve s3 object S3Object object = s3Client.getObject(new GetObjectRequest(s3Bucket, s3Key)); InputStream objectData = object.getObjectContent(); //Start putting your objects in AWS ES Service String esInput = "Build your JSON string here using S3 objectData"; HttpClient httpClient = new DefaultHttpClient(); HttpPut putRequest = new HttpPut(AWS_ES_ENDPOINT + "/{Index_name}/{product_name}/{unique_id}" ); StringEntity input = new StringEntity(esInput); input.setContentType("application/json"); putRequest.setEntity(input); httpClient.execute(putRequest); httpClient.getConnectionManager().shutdown(); } return "success";} 
  3. Use either Postman or Sense to create Actual index & corresponding mapping in ES. 使用Postman或Sense在ES中创建实际索引和相应的映射。

  4. Once done, download and run proxy.js on your machine. 完成后,在您的计算机上下载并运行proxy.js . Make sure you setup ES Security steps suggested in this post 在这建议一定要设置ES安全步骤

  5. Test setup and Kibana by running http://localhost:9200/_plugin/kibana/ URL from your machine. 通过从您的计算机运行http:// localhost:9200 / _plugin / kibana / URL来测试设置和Kibana。

  6. All is set. 一切都准备好了。 Go ahead and set your dashboard in Kibana. 继续在Kibana设置仪表板。 Test it by adding new objects in your S3 bucket 通过在S3存储桶中添加新对象来测试它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM