Skip to content

Commit ca698d2

Browse files
encoding url
1 parent 30246e0 commit ca698d2

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
2828
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
2929
import software.amazon.awssdk.services.s3.model.S3Exception;
30-
30+
31+
import java.io.UnsupportedEncodingException;
32+
import java.net.URLEncoder;
33+
import java.nio.charset.StandardCharsets;
34+
3135
// snippet-end:[s3.java2.copy_object.import]
3236
/**
3337
* Copy an object from one Amazon S3 bucket to another.
@@ -59,9 +63,15 @@ public static void main(String[] args)
5963
object_key, from_bucket, to_bucket);
6064
Region region = Region.US_WEST_2;
6165
S3Client s3 = S3Client.builder().region(region).build();
66+
String encodedUrl = null;
67+
try {
68+
encodedUrl = URLEncoder.encode(from_bucket + "/" + object_key, StandardCharsets.UTF_8.toString());
69+
} catch (UnsupportedEncodingException e) {
70+
System.out.println("URL could not be encoded: " + e.getMessage());
71+
}
6272

6373
CopyObjectRequest copyReq = CopyObjectRequest.builder()
64-
.copySource(from_bucket + "/" + object_key)
74+
.copySource(encodedUrl)
6575
.bucket(to_bucket)
6676
.key(object_key)
6777
.build();

0 commit comments

Comments
 (0)