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