Path: blob/master/code_examples/java_examples/S3Examples/HighLevelMultipartUpload.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;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.transfer.TransferManager;11import com.amazonaws.services.s3.transfer.TransferManagerBuilder;12import com.amazonaws.services.s3.transfer.Upload;1314public class HighLevelMultipartUpload {1516public static void main(String[] args) throws Exception {17String clientRegion = "*** Client region ***";18String bucketName = "*** Bucket name ***";19String keyName = "*** Object key ***";20String filePath = "*** Path for file to upload ***";2122try {23AmazonS3 s3Client = AmazonS3ClientBuilder.standard()24.withRegion(clientRegion)25.withCredentials(new ProfileCredentialsProvider())26.build();27TransferManager tm = TransferManagerBuilder.standard()28.withS3Client(s3Client)29.build();3031// TransferManager processes all transfers asynchronously,32// so this call returns immediately.33Upload upload = tm.upload(bucketName, keyName, new File(filePath));34System.out.println("Object upload started");3536// Optionally, wait for the upload to finish before continuing.37upload.waitForCompletion();38System.out.println("Object upload complete");39}40catch(AmazonServiceException e) {41// The call was transmitted successfully, but Amazon S3 couldn't process42// it, so it returned an error response.43e.printStackTrace();44}45catch(SdkClientException e) {46// Amazon S3 couldn't be contacted for a response, or the client47// couldn't parse the response from Amazon S3.48e.printStackTrace();49}50}51}5253