forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStorageAsyncExample.java
More file actions
119 lines (100 loc) · 5.06 KB
/
ObjectStorageAsyncExample.java
File metadata and controls
119 lines (100 loc) · 5.06 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 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.InputStream;
import java.util.concurrent.CountDownLatch;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorageAsync;
import com.oracle.bmc.objectstorage.ObjectStorageAsyncClient;
import com.oracle.bmc.objectstorage.model.BucketSummary;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.GetObjectRequest;
import com.oracle.bmc.objectstorage.requests.ListBucketsRequest;
import com.oracle.bmc.objectstorage.requests.ListBucketsRequest.Builder;
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;
import com.oracle.bmc.objectstorage.responses.GetObjectResponse;
import com.oracle.bmc.objectstorage.responses.ListBucketsResponse;
import com.oracle.bmc.responses.AsyncHandler;
public class ObjectStorageAsyncExample {
public static void main(String[] args) throws Exception {
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 AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
ObjectStorageAsync client = new ObjectStorageAsyncClient(provider);
client.setRegion(Region.US_PHOENIX_1);
ResponseHandler<GetNamespaceRequest, GetNamespaceResponse> namespaceHandler =
new ResponseHandler<>();
client.getNamespace(GetNamespaceRequest.builder().build(), namespaceHandler);
GetNamespaceResponse namespaceResponse = namespaceHandler.waitForCompletion();
String namespaceName = namespaceResponse.getValue();
System.out.println("Using namespace: " + namespaceName);
Builder listBucketsBuilder =
ListBucketsRequest.builder()
.namespaceName(namespaceName)
.compartmentId(provider.getTenantId());
String nextToken = null;
do {
listBucketsBuilder.page(nextToken);
ResponseHandler<ListBucketsRequest, ListBucketsResponse> listBucketsHandler =
new ResponseHandler<>();
client.listBuckets(listBucketsBuilder.build(), listBucketsHandler);
ListBucketsResponse listBucketsResponse = listBucketsHandler.waitForCompletion();
for (BucketSummary bucket : listBucketsResponse.getItems()) {
System.out.println("Found bucket: " + bucket.getName());
}
nextToken = listBucketsResponse.getOpcNextPage();
} while (nextToken != null);
// fetch the uploaded file from object storage
String bucketName = null;
String objectName = null;
ResponseHandler<GetObjectRequest, GetObjectResponse> objectHandler =
new ResponseHandler<>();
GetObjectRequest getObjectRequest =
GetObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.objectName(objectName)
.build();
client.getObject(getObjectRequest, objectHandler);
GetObjectResponse getResponse = objectHandler.waitForCompletion();
// stream contents should match the file uploaded
try (final InputStream fileStream = getResponse.getInputStream()) {
// use fileStream
} // try-with-resources automatically closes fileStream
client.close();
}
private static class ResponseHandler<IN, OUT> implements AsyncHandler<IN, OUT> {
private OUT item;
private Throwable failed = null;
private CountDownLatch latch = new CountDownLatch(1);
private OUT waitForCompletion() throws Exception {
latch.await();
if (failed != null) {
if (failed instanceof Exception) {
throw (Exception) failed;
}
throw (Error) failed;
}
return item;
}
@Override
public void onSuccess(IN request, OUT response) {
item = response;
latch.countDown();
}
@Override
public void onError(IN request, Throwable error) {
failed = error;
latch.countDown();
}
}
}