java - 使用 Java 中的 Lambda 将 CloudWatch 中的 AWS S3 事件发送到我的服务器

标签 java amazon-s3 aws-lambda apache-httpclient-4.x amazon-cloudwatch

我正在尝试将 S3 事件日志发送到我在 AWS 外部的服务器,以防在我的存储桶中使用 Lambda(上传、下载等)触发对象级 API 调用,但是当我运行它时,它返回超时而没有提到任何错误.我怀疑可能是因为它无法使用 HttpClient 将其发送到我的服务器但不完全确定。

这是我的 Lambda 函数:

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

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.event.S3EventNotification;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;

public class S3EventProcessor implements RequestHandler<S3Event, String> {

    public String handleRequest(S3Event s3event, Context context) {
        S3EventNotificationRecord record = s3event.getRecords().get(0);
        String event = record.toString();
        String url = "http://myexampleserver.com";



HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        StringEntity entity = null;
        try {
            entity = new StringEntity(event);
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        entity.setContentType("text/plain");
        post.setEntity(entity);
        context.getLogger().log("Setting up post request");

        HttpResponse response = null;
        try {
             response = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        context.getLogger().log(Integer.toString(response.getStatusLine().getStatusCode()));
        String a = "Event: " +  s3event.getRecords().get(0).getRequestParameters().getSourceIPAddress();
        return a;
    }

}

我的 Lambda 函数是否正确?

这是我的日志和错误消息:

START RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252 Version: $LATEST
Setting up post requestEND RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252
REPORT RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252  Duration: 30001.43 ms   Billed Duration: 30000 ms   Memory Size: 512 MB Max Memory Used: 73 MB  
2017-05-23T09:40:21.181Z bea1b956-3f9b-11e7-93c8-5503fb925252 Task timed out after 30.00 seconds
 "errorMessage": "2017-05-23T09:40:21.181Z bea1b956-3f9b-11e7-93c8-5503fb925252 Task timed out after 30.00 seconds"

这是我在我的 Lambda 中的 VPC 配置,因为我遵循 herehere : enter image description here

最佳答案

我不是在执行你的 lambda 函数。但是您的 lambda 函数看起来是正确的。

超过你无法触发下载对象的事件。

以下事件适用

RRSObjectLost, Put, Post, Copy, Complete Multipart Upload, Delete, Delete Marker Created, ObjectCreate (All), ObjectDelete (All).

通过从 lambda 控制台传递测试数据 S3 put 事件来测试您的 lambda。

关于java - 使用 Java 中的 Lambda 将 CloudWatch 中的 AWS S3 事件发送到我的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998130/

相关文章:

apache-spark - S3 强一致性是否意味着使用 S3 作为 Spark 结构化流应用程序的检查点位置是安全的?

amazon-web-services - 如何在 Typescript 中优雅地导入 AWS - Lambda?

python - 使用 Python 在 Lambda 中处理 S3 桶触发事件

java - 线程内一致性

amazon-web-services - 使用 Flask-admin 将图像上传到 Amazon S3

java - 在哪里放置一个接口(interface)相关的变量,它不是静态的和最终的

amazon-web-services - 如何使用 Elastic beanstalk 和 Dockerrun.aws.json 正确部署到 AWS?

node.js - AWS Lambda 函数 - 将 PDF 转换为图像

java - hadoop映射器静态初始化

java - Libgdx 矩形Arraylist仅最后一个作品