Path: blob/master/code_examples/java_examples/S3Examples/HighLevelTrackMultipartUpload.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.event.ProgressEvent;9import com.amazonaws.event.ProgressListener;10import com.amazonaws.services.s3.AmazonS3;11import com.amazonaws.services.s3.AmazonS3ClientBuilder;12import com.amazonaws.services.s3.model.PutObjectRequest;13import com.amazonaws.services.s3.transfer.TransferManager;14import com.amazonaws.services.s3.transfer.TransferManagerBuilder;15import com.amazonaws.services.s3.transfer.Upload;1617public class HighLevelTrackMultipartUpload {1819public static void main(String[] args) throws Exception {20String clientRegion = "*** Client region ***";21String bucketName = "*** Bucket name ***";22String keyName = "*** Object key ***";23String filePath = "*** Path to file to upload ***";2425try {26AmazonS3 s3Client = AmazonS3ClientBuilder.standard()27.withRegion(clientRegion)28.withCredentials(new ProfileCredentialsProvider())29.build();30TransferManager tm = TransferManagerBuilder.standard()31.withS3Client(s3Client)32.build();33PutObjectRequest request = new PutObjectRequest(bucketName, keyName, new File(filePath));3435// To receive notifications when bytes are transferred, add a36// ProgressListener to your request.37request.setGeneralProgressListener(new ProgressListener() {38public void progressChanged(ProgressEvent progressEvent) {39System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());40}41});42// TransferManager processes all transfers asynchronously,43// so this call returns immediately.44Upload upload = tm.upload(request);4546// Optionally, you can wait for the upload to finish before continuing.47upload.waitForCompletion();48}49catch(AmazonServiceException e) {50// The call was transmitted successfully, but Amazon S3 couldn't process51// it, so it returned an error response.52e.printStackTrace();53}54catch(SdkClientException e) {55// Amazon S3 couldn't be contacted for a response, or the client56// couldn't parse the response from Amazon S3.57e.printStackTrace();58}59}60}6162