Skip to content

Instantly share code, notes, and snippets.

@hajeriwalid
Created January 5, 2023 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hajeriwalid/349fe9558023acdbec9dd89988766e86 to your computer and use it in GitHub Desktop.
Save hajeriwalid/349fe9558023acdbec9dd89988766e86 to your computer and use it in GitHub Desktop.
package walidgroup003.walidapp003;
import java.io.InputStream;
import java.util.function.Supplier;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.ConfigFileReader.ConfigFile;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimplePrivateKeySupplier;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.BucketSummary;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.ListBucketsRequest;
import com.oracle.bmc.objectstorage.requests.ListBucketsRequest.Builder;
import com.oracle.bmc.objectstorage.responses.ListBucketsResponse;
/**
* Hello world!
*
*/
public class App {
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE = "DEFAULT";
public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
final ConfigFile config = ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE);
Supplier<InputStream> privateKeySupplierFromConfigEntry = new SimplePrivateKeySupplier(config.get("key_file"));
AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder()
.tenantId(config.get("tenancy")).userId(config.get("user")).fingerprint(config.get("fingerprint"))
.privateKeySupplier(privateKeySupplierFromConfigEntry).build();
String compartmentId = "ocid1.compartment.oc1..BLABLABLA";
ObjectStorageClient objectStorageClient = ObjectStorageClient.builder().build(provider);
objectStorageClient.setRegion(Region.EU_FRANKFURT_1); // You should modify to your own region
GetNamespaceRequest getNamespaceRequest = GetNamespaceRequest.builder().compartmentId(compartmentId).build();
String namespace = objectStorageClient.getNamespace(getNamespaceRequest).getValue();
System.out.println(
String.format("Object Storage namespace for compartment [%s] is [%s]", compartmentId, namespace));
Builder listBucketsBuilder = ListBucketsRequest.builder().namespaceName(namespace)
.compartmentId(provider.getTenantId());
String nextToken = null;
do {
listBucketsBuilder.page(nextToken).compartmentId(compartmentId);
ListBucketsResponse listBucketsResponse = objectStorageClient.listBuckets(listBucketsBuilder.build());
for (BucketSummary bucket : listBucketsResponse.getItems()) {
System.out.println("Found bucket: " + bucket.getName());
}
nextToken = listBucketsResponse.getOpcNextPage();
} while (nextToken != null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment