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