Skip to content

Commit 15de26e

Browse files
author
Bennett Lynch
committed
Update S3 CopyObject example to use sourceBucket & sourceKey parameters
A recent update to the AWS SDK for Java v2 (aws/aws-sdk-java-v2#2612) enabled support for more user-friendly parameters for specifying the source bucket, key, and version ID for the S3 CopyObject operation. These new parameters are also automatically URL-encoded by the SDK, as most users would expect. This commit updates the dependency to the latest version for the S3 module and takes advantage of the new parameters, simplifying the example code to no longer need to URL encode. Related: #740 #759
1 parent c298279 commit 15de26e

File tree

2 files changed

+3
-11
lines changed

2 files changed

+3
-11
lines changed

javav2/example_code/s3/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<dependency>
1515
<groupId>software.amazon.awssdk</groupId>
1616
<artifactId>bom</artifactId>
17-
<version>2.16.29</version>
17+
<version>2.17.4</version>
1818
<type>pom</type>
1919
<scope>import</scope>
2020
</dependency>

javav2/example_code/s3/src/main/java/com/example/s3/CopyObject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
2020
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
2121
import software.amazon.awssdk.services.s3.model.S3Exception;
22-
import java.io.UnsupportedEncodingException;
23-
import java.net.URLEncoder;
24-
import java.nio.charset.StandardCharsets;
2522
// snippet-end:[s3.java2.copy_object.import]
2623

2724
/**
@@ -68,14 +65,9 @@ public static void main(String[] args) {
6865
// snippet-start:[s3.java2.copy_object.main]
6966
public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) {
7067

71-
String encodedUrl = null;
72-
try {
73-
encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
74-
} catch (UnsupportedEncodingException e) {
75-
System.out.println("URL could not be encoded: " + e.getMessage());
76-
}
7768
CopyObjectRequest copyReq = CopyObjectRequest.builder()
78-
.copySource(encodedUrl)
69+
.sourceBucket(fromBucket)
70+
.sourceKey(objectKey)
7971
.destinationBucket(toBucket)
8072
.destinationKey(objectKey)
8173
.build();

0 commit comments

Comments
 (0)