4

In the version 1 SDK, making a copy request was straightforward with:

new CopyObjectRequest(sourceBucket, sourceKey, destinationBucket, destinationKey)

In the version 2 SDK, the Builder for CopyObjectRequest does not have a clear way to set the source vs destination. There is a copySource(copySource) method which accepts a full path, but there is no obvious way to set the destination bucket or destination key or to set the source bucket and source key normally (without building a full path and dealing with URL encoding).

Their new S3 examples simply leave out how the new copy works and their JavaDoc for CopyObjectRequest has no real information for this.

worpet
  • 3,788
  • 2
  • 31
  • 53
  • Note: Sometime after this was originally asked, AWS updated their S3 examples with an example of how the copy works. – worpet Dec 18 '19 at 17:41

3 Answers3

2

It follows the builder pattern now, so read the documentation for the CopyObjectRequest.Builder for more details.

Here's an example as of 'AWS SDK for Java' v2.17.166:

  s3.copyObject(
    CopyObjectRequest.builder()
      .sourceBucket(SOURCE_BUCKET_NAME)
      .sourceKey(SOURCE_KEY)
      .destinationBucket(DESTINATION_BUCKET_NAME)
      .destinationKey(DESTINATION_KEY)
      .build()
  );
xtratic
  • 4,600
  • 2
  • 14
  • 32
  • 1
    Their documentation wasn't helpful because they don't specify anywhere that `bucket` and `key` are intended for the destination values. I guess we are supposed to infer this since they have `copySource` with "source" in the name, but no methods with "destination" in the name. Seems a lot less clear than the old way though. – worpet Jun 19 '19 at 16:10
  • 1
    The `bucket` and `key` method have both been deprecated: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/CopyObjectRequest.Builder.html – james.garriss Dec 21 '20 at 14:45
  • @james.garriss I believe they were not deprecated at the time this was answered a year and a half ago. Thanks for letting me know, I've updated the answer. – xtratic Dec 21 '20 at 16:04
  • 1
    And now (at least in 2.17.165 version of AWS SDK for Java)...copySource(String) has been deprecated. Use `CopyObjectRequest.builder().sourceBucket(SOURCE_BUCKET_NAME).sourceKey(SOURCE_KEY).destinationBucket(DESTINATION_BUCKET_NAME).destinationKey(DESTINATION_KEY).build()` – Camille Apr 07 '22 at 14:55
  • @Camile Thank you, I've updated the answer to follow the API changes. – xtratic Apr 08 '22 at 20:07
2

They have a pretty good example here on Github: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/CopyObject.java

The_Cute_Hedgehog
  • 1,280
  • 13
  • 22
  • It seems like their example isn't fully correct since their [documentation](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/CopyObjectRequest.Builder.html#copySource-java.lang.String-) mentions the source value "must be URL-encoded", but they aren't encoding it here. Seems like it would fail if there are special characters in the key name. – worpet Jun 19 '19 at 16:18
  • Note: AWS eventually updated their example: https://github.com/awsdocs/aws-doc-sdk-examples/commit/ca698d2407e23351215de3cecadddbda8d1a137e#diff-04a531169b97e0279b78950d7d4f96b3 – worpet Dec 18 '19 at 17:42
  • Correct URL for updated example: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/CopyObject.java – james.garriss Dec 21 '20 at 14:46
0

At least in 2.17.165 version of AWS SDK for Java, copySource(String) has been deprecated.

Use

CopyObjectRequest.builder()
.sourceBucket(SOURCE_BUCKET_NAME)
.sourceKey(SOURCE_KEY)
.destinationBucket(DESTINATION_BUCKET_NAME)
.destinationKey(DESTINATION_KEY)
.build()
Camille
  • 2,439
  • 1
  • 14
  • 32