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/LowLevelMultipartUpload.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.File;
5
import java.io.IOException;
6
import java.util.ArrayList;
7
import java.util.List;
8
9
import com.amazonaws.AmazonServiceException;
10
import com.amazonaws.SdkClientException;
11
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
12
import com.amazonaws.services.s3.AmazonS3;
13
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
15
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
16
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
17
import com.amazonaws.services.s3.model.PartETag;
18
import com.amazonaws.services.s3.model.UploadPartRequest;
19
import com.amazonaws.services.s3.model.UploadPartResult;
20
21
public class LowLevelMultipartUpload {
22
23
public static void main(String[] args) throws IOException {
24
String clientRegion = "*** Client region ***";
25
String bucketName = "*** Bucket name ***";
26
String keyName = "*** Key name ***";
27
String filePath = "*** Path to file to upload ***";
28
29
File file = new File(filePath);
30
long contentLength = file.length();
31
long partSize = 5 * 1024 * 1024; // Set part size to 5 MB.
32
33
try {
34
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
35
.withRegion(clientRegion)
36
.withCredentials(new ProfileCredentialsProvider())
37
.build();
38
39
// Create a list of ETag objects. You retrieve ETags for each object part uploaded,
40
// then, after each individual part has been uploaded, pass the list of ETags to
41
// the request to complete the upload.
42
List<PartETag> partETags = new ArrayList<PartETag>();
43
44
// Initiate the multipart upload.
45
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
46
InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
47
48
// Upload the file parts.
49
long filePosition = 0;
50
for (int i = 1; filePosition < contentLength; i++) {
51
// Because the last part could be less than 5 MB, adjust the part size as needed.
52
partSize = Math.min(partSize, (contentLength - filePosition));
53
54
// Create the request to upload a part.
55
UploadPartRequest uploadRequest = new UploadPartRequest()
56
.withBucketName(bucketName)
57
.withKey(keyName)
58
.withUploadId(initResponse.getUploadId())
59
.withPartNumber(i)
60
.withFileOffset(filePosition)
61
.withFile(file)
62
.withPartSize(partSize);
63
64
// Upload the part and add the response's ETag to our list.
65
UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest);
66
partETags.add(uploadResult.getPartETag());
67
68
filePosition += partSize;
69
}
70
71
// Complete the multipart upload.
72
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
73
initResponse.getUploadId(), partETags);
74
s3Client.completeMultipartUpload(compRequest);
75
}
76
catch(AmazonServiceException e) {
77
// The call was transmitted successfully, but Amazon S3 couldn't process
78
// it, so it returned an error response.
79
e.printStackTrace();
80
}
81
catch(SdkClientException e) {
82
// Amazon S3 couldn't be contacted for a response, or the client
83
// couldn't parse the response from Amazon S3.
84
e.printStackTrace();
85
}
86
}
87
}
88