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