Skip to content

Fixing S3 Copy Object Java SDK v2 example to encode url #759

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 1 commit into from
Jul 22, 2019
Merged
Changes from all 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
Expand Up @@ -27,7 +27,11 @@
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

// snippet-end:[s3.java2.copy_object.import]
/**
* Copy an object from one Amazon S3 bucket to another.
Expand Down Expand Up @@ -59,9 +63,15 @@ public static void main(String[] args)
object_key, from_bucket, to_bucket);
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
String encodedUrl = null;
try {
encodedUrl = URLEncoder.encode(from_bucket + "/" + object_key, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("URL could not be encoded: " + e.getMessage());
}

CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(from_bucket + "/" + object_key)
.copySource(encodedUrl)
.bucket(to_bucket)
.key(object_key)
.build();
Expand Down