worpet
worpet

Reputation: 3893

AWS Java SDK 2 S3 copy object

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.

Upvotes: 4

Views: 4980

Answers (3)

xtratic
xtratic

Reputation: 4699

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()
  );

Upvotes: 2

Camille
Camille

Reputation: 2531

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()

Upvotes: 0

Related Questions