Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中为SNSEvent和S3Event使用多个lambdFunctionHandler?_Java_Amazon Web Services_Aws Lambda - Fatal编程技术网

如何在Java中为SNSEvent和S3Event使用多个lambdFunctionHandler?

如何在Java中为SNSEvent和S3Event使用多个lambdFunctionHandler?,java,amazon-web-services,aws-lambda,Java,Amazon Web Services,Aws Lambda,我想从s3事件和sns事件触发一个Lambda函数 当前版本如下: public class LambdaFunctionHandler implements RequestHandler<S3Event, Object> { public Object handleRequest(S3Event input, Context context) { context.getLogger().log("S3Event: " + input); return null

我想从s3事件和sns事件触发一个Lambda函数

当前版本如下:

public class LambdaFunctionHandler implements RequestHandler<S3Event, Object> {

  public Object handleRequest(S3Event input, Context context) {
    context.getLogger().log("S3Event: " + input);
    return null;
  }
}
public类LambdaFunctionHandler实现RequestHandler{
公共对象handleRequest(S3Event输入,上下文){
context.getLogger().log(“S3Event:+input”);
返回null;
}
}

是否有任何方法可以处理这两种事件类型?

作为一种方法,而不是使用
S3Event
类,您只需使用
Map
并根据请求对象的视图决定要做什么

但更合适的方法是如下。假设您有S3和SNS事件的公共逻辑,那么您正在使用这些请求中的一些公共属性。您可以创建自己的包含此公共属性的自定义基类,然后创建一个利用此基类的公共处理程序。另见:

粗略地说,它可能看起来像:

public class BaseCommonRequest {

     private String someCommonProperty1;

     // ...
} 

public class BaseLambdaFunctionHandler implements RequestHandler<BaseCommonRequest, Object> {

  public Object handleRequest(BaseCommonRequest input, Context context) {
    context.getLogger().log("event: " + input);
    return null;
  }
}
公共类BaseCommonRequest{
私有字符串someCommonProperty1;
// ...
} 
公共类BaselambdFunctionHandler实现RequestHandler{
公共对象handleRequest(BaseCommonRequest输入,上下文){
context.getLogger().log(“事件:+input”);
返回null;
}
}
如图所示,存在低级处理程序

public class Hello implements RequestStreamHandler{
    public static void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        int letter;
        while((letter = inputStream.read()) != -1)
        {
            outputStream.write(Character.toUpperCase(letter));
        }
    }
}

通过使用这些处理程序,我可以将请求转换为S3Event和SNSEvent。这里有一个示例代码。

谢谢。我更喜欢使用已经存在的S3Event和SNSEvent对象。这些事件实际上是JSON,我可以使用JSON解析器,但这会造成代码混乱。使用自己的类型并没有什么不好的。这是一种正常和预期的做法。此外,并非所有AWS事件在AWS Java API中都有相应的类