forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadManagerExample.java
More file actions
97 lines (82 loc) · 4.38 KB
/
DownloadManagerExample.java
File metadata and controls
97 lines (82 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.requests.GetObjectRequest;
import com.oracle.bmc.objectstorage.responses.GetObjectResponse;
import com.oracle.bmc.objectstorage.transfer.DownloadConfiguration;
import com.oracle.bmc.objectstorage.transfer.DownloadManager;
/**
* Example of using the DownloadManager to download objects.
* <p>
* DownloadManager can be configured to control how/when it does parallel uploads,
* and manages the underlying get requests and possible retries.
*
* Clients construct a GetObjectRequest similar to what they normally would.
*/
public class DownloadManagerExample {
public static void main(String[] args) throws Exception {
if (args.length != 4) {
throw new IllegalArgumentException(
"Syntax: java "
+ DownloadManagerExample.class.getName()
+ " <namespaceName> <bucketName> <objectName> <outputFileName>");
}
String namespaceName = args[0];
String bucketName = args[1];
String objectName = args[2];
String outputFileName = args[3];
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to the following
// line if needed and use ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final ConfigFileAuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
ObjectStorage client = new ObjectStorageClient(provider);
client.setRegion(Region.US_PHOENIX_1);
// configure download settings as desired
DownloadConfiguration downloadConfiguration =
DownloadConfiguration.builder()
.parallelDownloads(3)
.maxRetries(3)
.multipartDownloadThresholdInBytes(6 * 1024 * 1024)
.partSizeInBytes(4 * 1024 * 1024)
.build();
DownloadManager downloadManager = new DownloadManager(client, downloadConfiguration);
GetObjectRequest request =
GetObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.objectName(objectName)
.build();
// download request and print result
GetObjectResponse response = downloadManager.getObject(request);
System.out.println("Content length: " + response.getContentLength() + " bytes");
// use the stream contents; make sure to close the stream, e.g. by using try-with-resources
try (final InputStream stream = response.getInputStream();
final OutputStream outputStream = new FileOutputStream(outputFileName)) {
// use fileStream
byte[] buf = new byte[8192];
int bytesRead;
while ((bytesRead = stream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
} // try-with-resources automatically closes streams
System.out.println("File size: " + new File(outputFileName).length() + " bytes");
// or even simpler, if targetting a file:
response = downloadManager.downloadObjectToFile(request, new File(outputFileName));
System.out.println("Content length: " + response.getContentLength() + " bytes");
System.out.println("File size: " + new File(outputFileName).length() + " bytes");
}
}