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