Skip to content

Commit 2ccbcd6

Browse files
authored
Migration tool : TransferManager constructor and methods (#5920)
* Migration tool - TransferManager constructor and methods * Update dependency current version number * Separate end to end tests for TransferManager recipe * Rename recipe
1 parent 70b7438 commit 2ccbcd6

File tree

14 files changed

+702
-15
lines changed

14 files changed

+702
-15
lines changed

buildspecs/update-master-from-release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ phases:
4242
sed -i -E "s/(<awsjavasdk.previous.version>).+(<\/awsjavasdk.previous.version>)/\1$RELEASE_VERSION\2/" pom.xml
4343
sed -i -E "s/(newVersion: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
4444
sed -i -E "s/(version: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
45-
sed -i -E "s/(version: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-transfer-manager.yml
45+
sed -i -E "s/(version: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/add-transfer-manager-dependency.yml
4646
4747
git commit -am "Update to next snapshot version: $NEW_VERSION_SNAPSHOT"
4848
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.v2migrationtests;
17+
18+
import static java.util.Collections.addAll;
19+
import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure;
20+
import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion;
21+
import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion;
22+
import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion;
23+
import static software.amazon.awssdk.v2migrationtests.TestUtils.run;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.nio.file.Path;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import org.apache.commons.io.FileUtils;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.condition.EnabledIf;
34+
import software.amazon.awssdk.utils.Logger;
35+
36+
public class MavenTransferManagerTest {
37+
private static final Logger log = Logger.loggerFor(MavenTransferManagerTest.class);
38+
private static String sdkVersion;
39+
private static Path mavenTmBefore;
40+
private static Path mavenTmAfter;
41+
private static Path target;
42+
private static Path mavenTmActual;
43+
private static Path mavenTmExpected;
44+
45+
@BeforeAll
46+
static void setUp() throws IOException {
47+
sdkVersion = getVersion();
48+
mavenTmBefore = new File(MavenTransferManagerTest.class.getResource("maven-tm/before").getFile()).toPath();
49+
mavenTmAfter = new File(MavenTransferManagerTest.class.getResource("maven-tm/after").getFile()).toPath();
50+
target = new File(MavenTransferManagerTest.class.getResource("/").getFile()).toPath().getParent();
51+
52+
mavenTmActual = target.resolve("maven-tm/actual");
53+
mavenTmExpected = target.resolve("maven-tm/expected");
54+
55+
deleteTempDirectories();
56+
57+
FileUtils.copyDirectory(mavenTmBefore.toFile(), mavenTmActual.toFile());
58+
FileUtils.copyDirectory(mavenTmAfter.toFile(), mavenTmExpected.toFile());
59+
60+
replaceVersion(mavenTmExpected.resolve("pom.xml"), sdkVersion);
61+
replaceVersion(mavenTmActual.resolve("pom.xml"), sdkVersion);
62+
}
63+
64+
private static void deleteTempDirectories() throws IOException {
65+
FileUtils.deleteDirectory(mavenTmActual.toFile());
66+
FileUtils.deleteDirectory(mavenTmExpected.toFile());
67+
}
68+
69+
@Test
70+
@EnabledIf("versionAvailable")
71+
void mavenProject_shouldConvert() throws IOException {
72+
verifyTransformation();
73+
verifyCompilation();
74+
}
75+
76+
private static void verifyTransformation() throws IOException {
77+
List<String> rewriteArgs = new ArrayList<>();
78+
// pin version since updates have broken tests
79+
String rewriteMavenPluginVersion = "5.46.0";
80+
addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run",
81+
"-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW",
82+
"-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2Experimental");
83+
84+
run(mavenTmActual, rewriteArgs.toArray(new String[0]));
85+
FileUtils.deleteDirectory(mavenTmActual.resolve("target").toFile());
86+
assertTwoDirectoriesHaveSameStructure(mavenTmActual, mavenTmExpected);
87+
}
88+
89+
private static void verifyCompilation() {
90+
List<String> packageArgs = new ArrayList<>();
91+
addAll(packageArgs, "mvn", "package");
92+
run(mavenTmActual, packageArgs.toArray(new String[0]));
93+
}
94+
95+
boolean versionAvailable() {
96+
return TestUtils.versionAvailable(sdkVersion);
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>software.amazon.awssdk</groupId>
23+
<artifactId>sample-application</artifactId>
24+
<version>1.2.3</version>
25+
26+
<properties>
27+
<maven.compiler.source>1.8</maven.compiler.source>
28+
<maven.compiler.target>1.8</maven.compiler.target>
29+
</properties>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>software.amazon.awssdk</groupId>
35+
<artifactId>bom</artifactId>
36+
<version>V2_VERSION</version>
37+
<type>pom</type>
38+
<scope>import</scope>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>software.amazon.awssdk</groupId>
46+
<artifactId>s3</artifactId>
47+
<version>V2_VERSION</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>software.amazon.awssdk</groupId>
51+
<artifactId>s3-transfer-manager</artifactId>
52+
<version>V2_VERSION</version>
53+
</dependency>
54+
</dependencies>
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package foo.bar;
17+
18+
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
19+
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
20+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
21+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
22+
import software.amazon.awssdk.transfer.s3.S3TransferManager;
23+
import software.amazon.awssdk.transfer.s3.model.Copy;
24+
import software.amazon.awssdk.transfer.s3.model.CopyRequest;
25+
import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest;
26+
import software.amazon.awssdk.transfer.s3.model.FileDownload;
27+
import software.amazon.awssdk.transfer.s3.model.UploadFileRequest;
28+
29+
import java.io.File;
30+
import java.time.Duration;
31+
32+
public class TransferManagerS3 {
33+
34+
File file = new File("path/to/file.txt");
35+
36+
void tmConstructor() {
37+
S3TransferManager tm = S3TransferManager.builder()
38+
.build();
39+
S3TransferManager tmBuilderDefault = S3TransferManager.create();
40+
S3TransferManager tmBuilderWithS3 = S3TransferManager.builder().build();
41+
}
42+
43+
void download(S3TransferManager tm, String bucket, String key) {
44+
FileDownload download = tm.downloadFile(DownloadFileRequest.builder().getObjectRequest(GetObjectRequest.builder().bucket(bucket).key(key).build()).destination(file).build());
45+
46+
long timeout = 89;
47+
FileDownload download2 = tm.downloadFile(DownloadFileRequest.builder().getObjectRequest(GetObjectRequest.builder().bucket(bucket).key(key).overrideConfiguration(AwsRequestOverrideConfiguration.builder().apiCallTimeout(Duration.ofMillis(timeout)).build()).build()).destination(file).build());
48+
49+
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucket).key(key)
50+
.build();
51+
52+
FileDownload download3 = tm.downloadFile(DownloadFileRequest.builder().getObjectRequest(getObjectRequest).destination(file).build());
53+
54+
FileDownload download4 = tm.downloadFile(DownloadFileRequest.builder().getObjectRequest(getObjectRequest.toBuilder().overrideConfiguration(getObjectRequest.overrideConfiguration().get().toBuilder().apiCallTimeout(Duration.ofMillis(timeout)).build()).build()).destination(file).build());
55+
}
56+
57+
void upload(S3TransferManager tm, String bucket, String key) {
58+
tm.uploadFile(UploadFileRequest.builder().putObjectRequest(PutObjectRequest.builder().bucket(bucket).key(key).build()).source(file).build());
59+
}
60+
61+
void copy(S3TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
62+
Copy copy = tm.copy(CopyRequest.builder().copyObjectRequest(CopyObjectRequest.builder().sourceBucket(sourceBucket).sourceKey(sourceKey).destinationBucket(destinationBucket).destinationKey(destinationKey).build()).build());
63+
64+
CopyObjectRequest copyRequest = CopyObjectRequest.builder().sourceBucket(sourceBucket).sourceKey(sourceKey).destinationBucket(destinationBucket).destinationKey(destinationKey)
65+
.build();
66+
Copy copy2 = tm.copy(CopyRequest.builder().copyObjectRequest(copyRequest).build());
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>software.amazon.awssdk</groupId>
23+
<artifactId>sample-application</artifactId>
24+
<version>1.2.3</version>
25+
26+
<properties>
27+
<maven.compiler.source>1.8</maven.compiler.source>
28+
<maven.compiler.target>1.8</maven.compiler.target>
29+
</properties>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.amazonaws</groupId>
35+
<artifactId>aws-java-sdk-bom</artifactId>
36+
<version>1.11.1000</version>
37+
<type>pom</type>
38+
<scope>import</scope>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.amazonaws</groupId>
46+
<artifactId>aws-java-sdk-s3</artifactId>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package foo.bar;
17+
18+
import com.amazonaws.services.s3.model.CopyObjectRequest;
19+
import com.amazonaws.services.s3.model.GetObjectRequest;
20+
import com.amazonaws.services.s3.transfer.Copy;
21+
import com.amazonaws.services.s3.transfer.Download;
22+
import com.amazonaws.services.s3.transfer.TransferManager;
23+
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
24+
import java.io.File;
25+
26+
public class TransferManagerS3 {
27+
28+
File file = new File("path/to/file.txt");
29+
30+
void tmConstructor() {
31+
TransferManager tm = new TransferManager();
32+
TransferManager tmBuilderDefault = TransferManagerBuilder.defaultTransferManager();
33+
TransferManager tmBuilderWithS3 = TransferManagerBuilder.standard().build();
34+
}
35+
36+
void download(TransferManager tm, String bucket, String key) {
37+
Download download = tm.download(bucket, key, file);
38+
39+
long timeout = 89;
40+
Download download2 = tm.download(bucket, key, file, timeout);
41+
42+
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
43+
44+
Download download3 = tm.download(getObjectRequest, file);
45+
46+
Download download4 = tm.download(getObjectRequest, file, timeout);
47+
}
48+
49+
void upload(TransferManager tm, String bucket, String key) {
50+
tm.upload(bucket, key, file);
51+
}
52+
53+
void copy(TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
54+
Copy copy = tm.copy(sourceBucket, sourceKey, destinationBucket, destinationKey);
55+
56+
CopyObjectRequest copyRequest = new CopyObjectRequest(sourceBucket, sourceKey, destinationBucket, destinationKey);
57+
Copy copy2 = tm.copy(copyRequest);
58+
}
59+
}

0 commit comments

Comments
 (0)