forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStorageGetBucketExample.java
More file actions
117 lines (103 loc) · 5.62 KB
/
ObjectStorageGetBucketExample.java
File metadata and controls
117 lines (103 loc) · 5.62 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
/**
* 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 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.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.CreateBucketDetails;
import com.oracle.bmc.objectstorage.requests.CreateBucketRequest;
import com.oracle.bmc.objectstorage.requests.GetBucketRequest;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.GetBucketResponse;
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* Example to get a bucket and its statistics.
* <p>
* This example first creates a bucket in the compartment corresponding to the compartment OCID passed as the first
* argument to this program. The name of the bucket created is same as the second argument to the program. It also
* creates an object in this bucket whose name is the third argument.
* </p>
* It then illustrates how we can get a bucket and its statistics (Estimated Size and Estimated Count).
*
*
* @param args Arguments to provide to the example. The following arguments are expected:
* <ul>
* <li>The first argument is the OCID of the compartment.</li>
* <li>The second is the name of bucket to create and later fetch</li>
* <li>The third is the name of object to create inside bucket</li>
* </ul>
*/
public class ObjectStorageGetBucketExample {
public static void main(String[] args) throws Exception {
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
if (args.length != 3) {
throw new IllegalArgumentException(
"Unexpected number of arguments received. Consult the script header comments for expected arguments");
}
final String compartmentId = args[0];
final String bucket = args[1];
final String object = args[2];
// 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);
ObjectStorage client = new ObjectStorageClient(provider);
client.setRegion(Region.US_PHOENIX_1);
System.out.println("Getting the namespace.");
GetNamespaceResponse namespaceResponse =
client.getNamespace(GetNamespaceRequest.builder().build());
String namespaceName = namespaceResponse.getValue();
System.out.println("Creating the source bucket.");
CreateBucketDetails createSourceBucketDetails =
CreateBucketDetails.builder().compartmentId(compartmentId).name(bucket).build();
CreateBucketRequest createSourceBucketRequest =
CreateBucketRequest.builder()
.namespaceName(namespaceName)
.createBucketDetails(createSourceBucketDetails)
.build();
client.createBucket(createSourceBucketRequest);
System.out.println("Creating the source object");
PutObjectRequest putObjectRequest =
PutObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucket)
.objectName(object)
.contentLength(4L)
.putObjectBody(
new ByteArrayInputStream("data".getBytes(StandardCharsets.UTF_8)))
.build();
client.putObject(putObjectRequest);
System.out.println("Creating Get bucket request");
List<GetBucketRequest.Fields> fieldsList = new ArrayList<>(2);
fieldsList.add(GetBucketRequest.Fields.ApproximateCount);
fieldsList.add(GetBucketRequest.Fields.ApproximateSize);
GetBucketRequest request =
GetBucketRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucket)
.fields(fieldsList)
.build();
System.out.println("Fetching bucket details");
GetBucketResponse response = client.getBucket(request);
System.out.println("Bucket Name : " + response.getBucket().getName());
System.out.println("Bucket Compartment : " + response.getBucket().getCompartmentId());
System.out.println(
"The Approximate total number of objects within this bucket : "
+ response.getBucket().getApproximateCount());
System.out.println(
"The Approximate total size of objects within this bucket : "
+ response.getBucket().getApproximateSize());
}
}