There are more AWS SDK examples available in the AWS Doc SDK Examples
Use UploadPartCopy
with an AWS SDK
The following code example shows how to use UploadPartCopy
.
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create copy parts based on source object size and copy over individual parts to a directory bucket.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartCopyRequest; import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.completeDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Creates copy parts based on source object size and copies over individual * parts. * * @param s3Client The S3 client used to interact with S3 * @param sourceBucket The name of the source bucket * @param sourceKey The key (name) of the source object * @param destinationBucket The name of the destination bucket * @param destinationKey The key (name) of the destination object * @param uploadId The upload ID used to track the multipart upload * @return A list of completed parts */ public static List<CompletedPart> multipartUploadCopyForDirectoryBucket(S3Client s3Client, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey, String uploadId) { // Get the object size to track the end of the copy operation HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(sourceBucket) .key(sourceKey) .build(); HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest); long objectSize = headObjectResponse.contentLength(); logger.info("Source Object size: {}", objectSize); // Copy the object using 20 MB parts long partSize = 20 * 1024 * 1024; // 20 MB long bytePosition = 0; int partNum = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); while (bytePosition < objectSize) { long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); logger.info("Part Number: {}, Byte Position: {}, Last Byte: {}", partNum, bytePosition, lastByte); try { UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() .sourceBucket(sourceBucket) .sourceKey(sourceKey) .destinationBucket(destinationBucket) .destinationKey(destinationKey) .uploadId(uploadId) .copySourceRange("bytes=" + bytePosition + "-" + lastByte) .partNumber(partNum) .build(); UploadPartCopyResponse uploadPartCopyResponse = s3Client.uploadPartCopy(uploadPartCopyRequest); CompletedPart part = CompletedPart.builder() .partNumber(partNum) .eTag(uploadPartCopyResponse.copyPartResult().eTag()) .build(); uploadedParts.add(part); bytePosition += partSize; partNum++; } catch (S3Exception e) { logger.error("Failed to copy part number {}: {} - Error code: {}", partNum, e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } } return uploadedParts; }
-
For API details, see UploadPartCopy in AWS SDK for Java 2.x API Reference.
-
UploadPart
Scenarios