Skip to content

Added support for s3-event-notifications in migration tool #6059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.v2migrationtests;

import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class MavenS3EventNotificationTest extends MavenTestBase {

@BeforeAll
static void setUp() throws IOException {
mavenBefore = new File(MavenS3EventNotificationTest.class.getResource("maven-en/before").getFile()).toPath();
mavenAfter = new File(MavenS3EventNotificationTest.class.getResource("maven-en/after").getFile()).toPath();
target = new File(MavenS3EventNotificationTest.class.getResource("/").getFile()).toPath().getParent();

mavenActual = target.resolve("maven-en/actual");
mavenExpected = target.resolve("maven-en/expected");

deleteTempDirectories();

FileUtils.copyDirectory(mavenBefore.toFile(), mavenActual.toFile());
FileUtils.copyDirectory(mavenAfter.toFile(), mavenExpected.toFile());

replaceVersion(mavenExpected.resolve("pom.xml"), sdkVersion);
replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion);
}

@Test
@EnabledIf("versionAvailable")
void mavenProject_shouldConvert() throws IOException {
boolean experimental = false;
verifyTransformation(experimental);
verifyCompilation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License").
~ You may not use this file except in compliance with the License.
~ A copy of the License is located at
~
~ http://aws.amazon.com/apache2.0
~
~ or in the "license" file accompanying this file. This file is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
~ express or implied. See the License for the specific language governing
~ permissions and limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>software.amazon.awssdk</groupId>
<artifactId>sample-application</artifactId>
<version>1.2.3</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>V2_VERSION</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>V2_VERSION</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.5</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-event-notifications</artifactId>
<version>V2_VERSION</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package foo.bar;

import software.amazon.awssdk.eventnotifications.s3.model.S3Bucket;
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification;
import software.amazon.awssdk.eventnotifications.s3.model.S3;
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotificationRecord;
import software.amazon.awssdk.eventnotifications.s3.model.S3Object;
import software.amazon.awssdk.eventnotifications.s3.model.RequestParameters;
import software.amazon.awssdk.eventnotifications.s3.model.ResponseElements;
import software.amazon.awssdk.eventnotifications.s3.model.RestoreEventData;
import software.amazon.awssdk.eventnotifications.s3.model.UserIdentity;
import software.amazon.awssdk.eventnotifications.s3.model.GlacierEventData;
import software.amazon.awssdk.eventnotifications.s3.model.LifecycleEventData;
import software.amazon.awssdk.eventnotifications.s3.model.IntelligentTieringEventData;
import software.amazon.awssdk.eventnotifications.s3.model.ReplicationEventData;
import org.joda.time.DateTime;

public class S3EventNotificationTest {
public void parseEvent(String jsonInput) {
S3EventNotification notification = S3EventNotification.fromJson(jsonInput);

for (S3EventNotificationRecord record : notification.getRecords()) {
S3 s3 = record.getS3();

S3Bucket bucket = s3.getBucket();

S3Object object = s3.getObject();

String eventName = record.getEventName();

String eventNameEnum = record.getEventName();

DateTime eventTime = /*AWS SDK for Java v2 migration: getEventTime returns Instant instead of DateTime in v2. AWS SDK v2 does not include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to manually add "org.joda.time:joda-time" dependency to your project after migration.*/new DateTime(record.getEventTime().toEpochMilli());

RequestParameters requestParams = record.getRequestParameters();

ResponseElements responseElements = record.getResponseElements();

UserIdentity userIdentity = record.getUserIdentity();

GlacierEventData glacierEventData = record.getGlacierEventData();

RestoreEventData restoreEventData = glacierEventData.getRestoreEventData();

DateTime expireTime = /*AWS SDK for Java v2 migration: getLifecycleRestorationExpiryTime returns Instant instead of DateTime in v2. AWS SDK v2 does not include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to manually add "org.joda.time:joda-time" dependency to your project after migration.*/new DateTime(restoreEventData.getLifecycleRestorationExpiryTime().toEpochMilli());

LifecycleEventData lifecycleEventData = record.getLifecycleEventData();

IntelligentTieringEventData intelligentTieringEventData = record.getIntelligentTieringEventData();

ReplicationEventData replicationEventData = record.getReplicationEventData();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License").
~ You may not use this file except in compliance with the License.
~ A copy of the License is located at
~
~ http://aws.amazon.com/apache2.0
~
~ or in the "license" file accompanying this file. This file is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
~ express or implied. See the License for the specific language governing
~ permissions and limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>software.amazon.awssdk</groupId>
<artifactId>sample-application</artifactId>
<version>1.2.3</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.12.782</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package foo.bar;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.event.S3EventNotification;
import com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity;
import com.amazonaws.services.s3.event.S3EventNotification.S3Entity;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
import com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity;
import com.amazonaws.services.s3.event.S3EventNotification.RequestParametersEntity;
import com.amazonaws.services.s3.event.S3EventNotification.ResponseElementsEntity;
import com.amazonaws.services.s3.event.S3EventNotification.RestoreEventDataEntity;
import com.amazonaws.services.s3.event.S3EventNotification.UserIdentityEntity;
import com.amazonaws.services.s3.event.S3EventNotification.GlacierEventDataEntity;
import com.amazonaws.services.s3.event.S3EventNotification.LifecycleEventDataEntity;
import com.amazonaws.services.s3.event.S3EventNotification.IntelligentTieringEventDataEntity;
import com.amazonaws.services.s3.event.S3EventNotification.ReplicationEventDataEntity;
import com.amazonaws.services.s3.model.S3Event;
import org.joda.time.DateTime;

public class S3EventNotificationTest {
public void parseEvent(String jsonInput) {
S3EventNotification notification = S3EventNotification.parseJson(jsonInput);

for (S3EventNotificationRecord record : notification.getRecords()) {
S3Entity s3 = record.getS3();

S3BucketEntity bucket = s3.getBucket();

S3ObjectEntity object = s3.getObject();

String eventName = record.getEventName();

S3Event eventNameEnum = record.getEventNameAsEnum();

DateTime eventTime = record.getEventTime();

RequestParametersEntity requestParams = record.getRequestParameters();

ResponseElementsEntity responseElements = record.getResponseElements();

UserIdentityEntity userIdentity = record.getUserIdentity();

GlacierEventDataEntity glacierEventData = record.getGlacierEventData();

RestoreEventDataEntity restoreEventData = glacierEventData.getRestoreEventData();

DateTime expireTime = restoreEventData.getLifecycleRestorationExpiryTime();

LifecycleEventDataEntity lifecycleEventData = record.getLifecycleEventData();

IntelligentTieringEventDataEntity intelligentTieringEventData = record.getIntelligentTieringEventData();

ReplicationEventDataEntity replicationEventData = record.getReplicationEventDataEntity();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_MODEL_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1EnMethodMatcher;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1S3MethodMatcher;

import java.util.List;
Expand Down Expand Up @@ -52,6 +53,10 @@ public class S3AddImportsAndComments extends Recipe {
private static final MethodMatcher SELECT_OBJECT_CONTENT = v1S3MethodMatcher("selectObjectContent(..)");
private static final MethodMatcher SET_LIFECYCLE_CONFIGURATION = v1S3MethodMatcher("setBucketLifecycleConfiguration(..)");
private static final MethodMatcher SET_TAGGING_CONFIGURATION = v1S3MethodMatcher("setBucketTaggingConfiguration(..)");
private static final MethodMatcher GET_EVENT_TIME = v1EnMethodMatcher("S3EventNotification.S3EventNotificationRecord "
+ "getEventTime(..)");
private static final MethodMatcher GET_EXPIRY_TIME = v1EnMethodMatcher("S3EventNotification.RestoreEventDataEntity "
+ "getLifecycleRestorationExpiryTime(..)");


private static final Pattern CANNED_ACL = Pattern.compile(V1_S3_MODEL_PKG + "CannedAccessControlList");
Expand Down Expand Up @@ -194,6 +199,13 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return method.withComments(createComments(comment));
}

if (GET_EVENT_TIME.matches(method) || GET_EXPIRY_TIME.matches(method)) {
String comment = method.getSimpleName() + " returns Instant instead of DateTime in v2. AWS SDK v2 does not "
+ "include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to "
+ "manually add \"org.joda.time:joda-time\" dependency to your"
+ " project after migration.";
return method.withComments(createComments(comment));
}
return method;
}

Expand Down
Loading
Loading