From aa399f58791eaf0273c3780a7b5f477806a55731 Mon Sep 17 00:00:00 2001 From: Martin Merfort <395822+mmerfort@users.noreply.github.com> Date: Mon, 29 Jul 2024 13:13:57 +0200 Subject: [PATCH] Add S3BatchEventV2 (#496) * Add S3BatchEventV2 class * Add test for S3BatchEventV2 --------- Co-authored-by: Martin Merfort Co-authored-by: Alexander Smirnov <75367056+smirnoal@users.noreply.github.com> --- .../lambda/runtime/events/S3BatchEventV2.java | 50 +++++++++++++++++++ .../runtime/events/S3BatchResponse.java | 8 ++- .../lambda/runtime/tests/EventLoader.java | 4 ++ .../runtime/tests/S3BatchEventV2Test.java | 20 ++++++++ .../src/test/resources/s3_batch_event_v2.json | 21 ++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchEventV2.java create mode 100644 aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/S3BatchEventV2Test.java create mode 100644 aws-lambda-java-tests/src/test/resources/s3_batch_event_v2.json diff --git a/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchEventV2.java b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchEventV2.java new file mode 100644 index 00000000..92b8722d --- /dev/null +++ b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchEventV2.java @@ -0,0 +1,50 @@ +package com.amazonaws.services.lambda.runtime.events; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +/** + * Event to represent the payload which is sent to Lambda by S3 Batch to perform a custom + * action when using invocation schema version 2.0. + * + * https://docs.aws.amazon.com/AmazonS3/latest/dev/batch-ops-invoke-lambda.html + */ + +@Data +@Builder(setterPrefix = "with") +@NoArgsConstructor +@AllArgsConstructor +public class S3BatchEventV2 { + + private String invocationSchemaVersion; + private String invocationId; + private Job job; + private List tasks; + + @Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Job { + + private String id; + private Map userArguments; + } + + @Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Task { + + private String taskId; + private String s3Key; + private String s3VersionId; + private String s3BucketName; + } +} diff --git a/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchResponse.java b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchResponse.java index 4fdd1273..d584a31d 100644 --- a/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchResponse.java +++ b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/S3BatchResponse.java @@ -62,4 +62,10 @@ public static S3BatchResponseBuilder fromS3BatchEvent(S3BatchEvent s3BatchEvent) .withInvocationId(s3BatchEvent.getInvocationId()) .withInvocationSchemaVersion(s3BatchEvent.getInvocationSchemaVersion()); } -} \ No newline at end of file + + public static S3BatchResponseBuilder fromS3BatchEvent(S3BatchEventV2 s3BatchEvent) { + return S3BatchResponse.builder() + .withInvocationId(s3BatchEvent.getInvocationId()) + .withInvocationSchemaVersion(s3BatchEvent.getInvocationSchemaVersion()); + } +} diff --git a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java index 77812267..0c5d6620 100644 --- a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java +++ b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java @@ -105,6 +105,10 @@ public static S3Event loadS3Event(String filename) { return loadEvent(filename, S3Event.class); } + public static S3BatchEventV2 loadS3BatchEventV2(String filename) { + return loadEvent(filename, S3BatchEventV2.class); + } + public static SecretsManagerRotationEvent loadSecretsManagerRotationEvent(String filename) { return loadEvent(filename, SecretsManagerRotationEvent.class); } diff --git a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/S3BatchEventV2Test.java b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/S3BatchEventV2Test.java new file mode 100644 index 00000000..2c9913dc --- /dev/null +++ b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/S3BatchEventV2Test.java @@ -0,0 +1,20 @@ +package com.amazonaws.services.lambda.runtime.tests; + +import com.amazonaws.services.lambda.runtime.events.S3BatchEventV2; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class S3BatchEventV2Test { + + @Test + public void testS3BatchEventV2() { + S3BatchEventV2 event = EventLoader.loadS3BatchEventV2("s3_batch_event_v2.json"); + assertThat(event).isNotNull(); + assertThat(event.getInvocationId()).isEqualTo("Jr3s8KZqYWRmaiBhc2RmdW9hZHNmZGpmaGFzbGtkaGZzatx7Ruy"); + assertThat(event.getJob()).isNotNull(); + assertThat(event.getJob().getUserArguments().get("MyDestinationBucket")).isEqualTo("destination-directory-bucket-name"); + assertThat(event.getTasks()).hasSize(1); + assertThat(event.getTasks().get(0).getS3Key()).isEqualTo("s3objectkey"); + } +} diff --git a/aws-lambda-java-tests/src/test/resources/s3_batch_event_v2.json b/aws-lambda-java-tests/src/test/resources/s3_batch_event_v2.json new file mode 100644 index 00000000..4cdbacaa --- /dev/null +++ b/aws-lambda-java-tests/src/test/resources/s3_batch_event_v2.json @@ -0,0 +1,21 @@ +{ + "invocationSchemaVersion": "2.0", + "invocationId": "Jr3s8KZqYWRmaiBhc2RmdW9hZHNmZGpmaGFzbGtkaGZzatx7Ruy", + "job": { + "id": "ry77cd60-61f6-4a2b-8a21-d07600c874gf", + "userArguments": { + "MyDestinationBucket": "destination-directory-bucket-name", + "MyDestinationBucketRegion": "us-east-1", + "MyDestinationPrefix": "copied/", + "MyDestinationObjectKeySuffix": "_new_suffix" + } + }, + "tasks": [ + { + "taskId": "y5R3a2lkZ29lc2hlurcS", + "s3Key": "s3objectkey", + "s3VersionId": null, + "s3Bucket": "source-directory-bucket-name" + } + ] +}