Path: blob/master/code_examples/java_examples/S3Examples/LowLevelMultipartCopy.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;4import java.util.ArrayList;5import java.util.List;67import com.amazonaws.AmazonServiceException;8import com.amazonaws.SdkClientException;9import com.amazonaws.auth.profile.ProfileCredentialsProvider;10import com.amazonaws.services.s3.*;11import com.amazonaws.services.s3.model.*;1213public class LowLevelMultipartCopy {1415public static void main(String[] args) throws IOException {16String clientRegion = "*** Client region ***";17String sourceBucketName = "*** Source bucket name ***";18String sourceObjectKey = "*** Source object key ***";19String destBucketName = "*** Target bucket name ***";20String destObjectKey = "*** Target object key ***";2122try {23AmazonS3 s3Client = AmazonS3ClientBuilder.standard()24.withCredentials(new ProfileCredentialsProvider())25.withRegion(clientRegion)26.build();2728// Initiate the multipart upload.29InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(destBucketName, destObjectKey);30InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initRequest);3132// Get the object size to track the end of the copy operation.33GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey);34ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest);35long objectSize = metadataResult.getContentLength();3637// Copy the object using 5 MB parts.38long partSize = 5 * 1024 * 1024;39long bytePosition = 0;40int partNum = 1;41List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>();42while (bytePosition < objectSize) {43// The last part might be smaller than partSize, so check to make sure44// that lastByte isn't beyond the end of the object.45long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1);4647// Copy this part.48CopyPartRequest copyRequest = new CopyPartRequest()49.withSourceBucketName(sourceBucketName)50.withSourceKey(sourceObjectKey)51.withDestinationBucketName(destBucketName)52.withDestinationKey(destObjectKey)53.withUploadId(initResult.getUploadId())54.withFirstByte(bytePosition)55.withLastByte(lastByte)56.withPartNumber(partNum++);57copyResponses.add(s3Client.copyPart(copyRequest));58bytePosition += partSize;59}6061// Complete the upload request to concatenate all uploaded parts and make the copied object available.62CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(63destBucketName,64destObjectKey,65initResult.getUploadId(),66getETags(copyResponses));67s3Client.completeMultipartUpload(completeRequest);68System.out.println("Multipart copy complete.");69}70catch(AmazonServiceException e) {71// The call was transmitted successfully, but Amazon S3 couldn't process72// it, so it returned an error response.73e.printStackTrace();74}75catch(SdkClientException e) {76// Amazon S3 couldn't be contacted for a response, or the client77// couldn't parse the response from Amazon S3.78e.printStackTrace();79}80}8182// This is a helper function to construct a list of ETags.83private static List<PartETag> getETags(List<CopyPartResult> responses) {84List<PartETag> etags = new ArrayList<PartETag>();85for (CopyPartResult response : responses) {86etags.add(new PartETag(response.getPartNumber(), response.getETag()));87}88return etags;89}90}919293