AWS Lambda S3Event反序列化

时间:2016-04-19 22:01:12

标签: java amazon-web-services aws-lambda

有没有人成功为S3事件实现了基于Java的请求处理程序?

我的课程:

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.model.S3Event;

public class Hello implements RequestHandler<S3Event, String> {
    public String handleRequest(S3Event event, Context context) {
        return "Success";
    }
}

错误讯息:

JSON解析期间发生错误:java.lang.RuntimeException java.lang.RuntimeException:JSON解析期间发生错误由以下引起:java.io.UncheckedIOException:com.fasterxml.jackson.databind.JsonMappingException:Can不要在[Source:lambdainternal.util.NativeMemoryAsInputStream@6108b2d7;中的START_OBJECT标记中反序列化com.amazonaws.services.s3.model.S3Event的实例。 line:1,column:1]引起:com.fasterxml.jackson.databind.JsonMappingException:无法在[Source:lambdainternal.util.NativeMemoryAsInputStream]的START_OBJECT标记中反序列化com.amazonaws.services.s3.model.S3Event的实例@ 6108b2d7; com:fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)中的com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)at com.fasterxml中的行:1,列:1] .jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)位于com.fasterxml.jackson.databind.deser.std的com.fasterxml.jackson.databind.deser.std.EnumDeserializer._deserializeOther(EnumDeserializer.java:137) .inumDeserializer.deserialize(EnumDeserializer.java:89)at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:18)at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java) :1441)at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1047)

1 个答案:

答案 0 :(得分:10)

解决方案是采取S3EventNotification。希望AWS文档中包含这些内容。我喜欢Lambda概念,但那里没有太多文档或示例。

public void mergeGLAU(S3EventNotification notification, Context context) {
    try {
        for (S3EventNotificationRecord record : notification.getRecords()) {
            System.out.println(record.getEventSource());
            System.out.println(record.getEventName());
            System.out.println(record.getS3().getBucket().getName());
            System.out.println(record.getS3().getObject().getKey());
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}