Spring Cloud Function Was S3Event 错误转换通用消息

Spring Cloud Function Was S3Event error converting generic message

我正在使用最新版本的 Spring Cloud Function,如下所示

<properties>
    <java.version>8</java.version>
    <spring-cloud.version>2020.0.1</spring-cloud.version>
    <aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
    <aws-lambda-java-events.version>3.7.0</aws-lambda-java-events.version>
    <aws-java-sdk-s3.version>1.11.948</aws-java-sdk-s3.version>
    <wrapper.version>1.0.17.RELEASE</wrapper.version>
</properties>
<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-context</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-adapter-aws</artifactId>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>${aws-java-sdk-s3.version}</version>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>${aws-lambda-java-core.version}</version>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>${aws-lambda-java-events.version}</version>
    </dependency>

这是主要的class

public static void main(String[] args) {
    System.out.println("Application started");
    SpringApplication.run(Application.class, args);
}


@Bean
public Function<S3Event, String> extractFileName() {
    return s3Event -> {
        return s3Event.getRecords().get(0).getS3().getBucket().getName();
    };
}

我是 运行 使用 AWS SAM 的应用程序

sam local invoke AwsLambdaS3Local --log-file ./output.log -e event.json

event.json内容是

{"Records":[{"eventVersion":"2.0","eventTime":"1970-01-01T00:00:00.000Z","requestParameters":{"sourceIPAddress":"127.0.0.1"},"s3":{"configurationId":"testConfigRule","object":{"eTag":"0123456789abcdef0123456789abcdef","sequencer":"0A1B2C3D4E5F678901","key":"test.txt","size":1024},"bucket":{"arn":"arn:aws:s3:::demo_bucket","name":"demo_bucket","ownerIdentity":{"principalId":"EXAMPLE"}},"s3SchemaVersion":"1.0"},"responseElements":{"x-amz-id-2":"EXAMPLE123\/5678abcdefghijklambdaisawesome\/mnopqrstuvwxyzABCDEFGH","x-amz-request-id":"EXAMPLE123456789"},"awsRegion":"us-east-1","eventName":"ObjectCreated:Put","userIdentity":{"principalId":"EXAMPLE"},"eventSource":"aws:s3"}]}

应用程序启动时,我看到以下错误

Caused by: java.lang.IllegalArgumentException: Failed to convert input: GenericMessage [payload=byte[676], headers={date=Fri, 05 Feb 2021 12:59:47 GMT, content-length=676, lambda-runtime-aws-request-id=3a84db79-8fc8-44e3-b223-63d20008cf8c, id=5bde8b80-a881-4c4d-222b-8d0a46c0cb55, contentType=application/json, lambda-runtime-deadline-ms=3225038924621, timestamp=1612529988064}] to class com.amazonaws.services.lambda.runtime.events.S3Event

有什么想法吗? 您有 Spring Cloud Function 2020.0.1 工作的示例吗?

谢谢

再挖掘一些,你的版本不匹配。 基本上目前我们依赖于 AWS

的以下依赖项
<aws-lambda-events.version>2.2.6</aws-lambda-events.version>
<aws-java-sdk.version>1.11.825</aws-java-sdk.version>

您正在使用

<aws-lambda-java-events.version>3.7.0</aws-lambda-java-events.version>
<aws-java-sdk-s3.version>1.11.948</aws-java-sdk-s3.version>

不确定所有细节,但我看到 S3EEvent 的结构已更改。换句话说,AWS S3Event 发生了重大变化,您的实际 event.json 与它不兼容。 您可以使用以下代码轻松验证它

try {
    ObjectMapper mapper = new ObjectMapper();
    mapper.readValue(<get bytes from your event.json>, S3Event.class);
}
catch (Exception e) {
    e.printStackTrace();
}