|
| 1 | +package cloud.stackit.sdk.objectstorage.examples; |
| 2 | + |
| 3 | +import cloud.stackit.sdk.core.KeyFlowAuthenticator; |
| 4 | +import cloud.stackit.sdk.core.config.CoreConfiguration; |
| 5 | +import cloud.stackit.sdk.core.exception.ApiException; |
| 6 | +import cloud.stackit.sdk.objectstorage.api.ObjectStorageApi; |
| 7 | +import cloud.stackit.sdk.objectstorage.model.*; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.HttpURLConnection; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.OffsetDateTime; |
| 12 | +import java.util.List; |
| 13 | +import okhttp3.OkHttpClient; |
| 14 | + |
| 15 | +@SuppressWarnings("PMD.CouplingBetweenObjects") |
| 16 | +final class ObjectStorageExample { |
| 17 | + |
| 18 | + private ObjectStorageExample() {} |
| 19 | + |
| 20 | + @SuppressWarnings({ |
| 21 | + "PMD.CyclomaticComplexity", |
| 22 | + "PMD.CognitiveComplexity", |
| 23 | + "PMD.NPathComplexity", |
| 24 | + "PMD.NcssCount", |
| 25 | + "PMD.SystemPrintln", |
| 26 | + "PMD.AvoidThrowingRawExceptionTypes" |
| 27 | + }) |
| 28 | + public static void main(String[] args) throws IOException { |
| 29 | + // Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env |
| 30 | + // STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY |
| 31 | + CoreConfiguration configuration = new CoreConfiguration(); |
| 32 | + |
| 33 | + OkHttpClient httpClient = new OkHttpClient(); |
| 34 | + KeyFlowAuthenticator authenticator = new KeyFlowAuthenticator(httpClient, configuration); |
| 35 | + httpClient = |
| 36 | + httpClient |
| 37 | + .newBuilder() |
| 38 | + .authenticator(authenticator) |
| 39 | + // The object storage requests are sychronous and may take a few seconds. |
| 40 | + // To prevent an timeout, we increase the read timeout to 15 seconds |
| 41 | + .readTimeout(Duration.ofSeconds(15)) |
| 42 | + .build(); |
| 43 | + |
| 44 | + ObjectStorageApi objectStorageApi = new ObjectStorageApi(httpClient); |
| 45 | + |
| 46 | + // the id of your STACKIT project, read from env var for this example |
| 47 | + String projectId = System.getenv("STACKIT_PROJECT_ID"); |
| 48 | + if (projectId == null || projectId.isEmpty()) { |
| 49 | + System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found."); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // the region which should be used to interact with objectstorage |
| 54 | + String region = "eu01"; |
| 55 | + |
| 56 | + try { |
| 57 | + /* |
| 58 | + * /////////////////////////////////////////////////////// |
| 59 | + * // P R O J E C T E N A B L E M E N T // |
| 60 | + * /////////////////////////////////////////////////////// |
| 61 | + */ |
| 62 | + /* check if object storage is enabled for the project */ |
| 63 | + ProjectStatus projectStatus; |
| 64 | + System.out.println("Checking if object storage is enabled"); |
| 65 | + try { |
| 66 | + projectStatus = objectStorageApi.getServiceStatus(projectId, region); |
| 67 | + System.out.println("* Object storage is enabled"); |
| 68 | + } catch (ApiException e) { |
| 69 | + // If response status is not found, object storage is not enabled for the |
| 70 | + // project |
| 71 | + if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { |
| 72 | + System.out.println("* Object storage is not enabled for the project"); |
| 73 | + System.out.println("* Enabling object storage"); |
| 74 | + projectStatus = objectStorageApi.enableService(projectId, region); |
| 75 | + System.out.println("* Object storage successful enabled for the project"); |
| 76 | + } else { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + ProjectScope scope = projectStatus.getScope(); |
| 81 | + System.out.println("* Scope of the object storage: " + scope); |
| 82 | + |
| 83 | + /* |
| 84 | + * /////////////////////////////////////////////////////// |
| 85 | + * // B U C K E T S // |
| 86 | + * /////////////////////////////////////////////////////// |
| 87 | + * */ |
| 88 | + |
| 89 | + /* create a new bucket in the project */ |
| 90 | + System.out.println("\nCreating bucket"); |
| 91 | + CreateBucketResponse newBucket = |
| 92 | + objectStorageApi.createBucket(projectId, region, "java-sdk-example"); |
| 93 | + System.out.println(" * Bucket name: " + newBucket.getBucket()); |
| 94 | + System.out.println(" * Project ID: " + newBucket.getProject()); |
| 95 | + |
| 96 | + /* list all available buckets in the project */ |
| 97 | + System.out.println("\nListing all buckets:"); |
| 98 | + ListBucketsResponse fetchedBuckets = objectStorageApi.listBuckets(projectId, region); |
| 99 | + List<Bucket> listBuckets = fetchedBuckets.getBuckets(); |
| 100 | + for (Bucket bucket : listBuckets) { |
| 101 | + System.out.println("*******************"); |
| 102 | + System.out.println("* Bucket name: " + bucket.getName()); |
| 103 | + System.out.println("* Virtual host: " + bucket.getUrlPathStyle()); |
| 104 | + } |
| 105 | + |
| 106 | + /* deleting the bucket we just created */ |
| 107 | + System.out.println("\nDeleting bucket:"); |
| 108 | + DeleteBucketResponse deleteBucketResponse = |
| 109 | + objectStorageApi.deleteBucket(projectId, region, newBucket.getBucket()); |
| 110 | + System.out.println("* Bucket name: " + deleteBucketResponse.getBucket()); |
| 111 | + System.out.println("* Bucket successfully deleted"); |
| 112 | + |
| 113 | + /* |
| 114 | + * /////////////////////////////////////////////////////// |
| 115 | + * // C R E D E N T I A L G R O U P // |
| 116 | + * /////////////////////////////////////////////////////// |
| 117 | + * */ |
| 118 | + |
| 119 | + /* creating a new credential group in the project */ |
| 120 | + System.out.println("\nCreating credential group:"); |
| 121 | + CreateCredentialsGroupResponse newCredentialGroup = |
| 122 | + objectStorageApi.createCredentialsGroup( |
| 123 | + projectId, |
| 124 | + region, |
| 125 | + new CreateCredentialsGroupPayload().displayName("java-sdk-example")); |
| 126 | + System.out.println( |
| 127 | + "* Group ID" |
| 128 | + + newCredentialGroup.getCredentialsGroup().getCredentialsGroupId()); |
| 129 | + System.out.println( |
| 130 | + "* Display name" + newCredentialGroup.getCredentialsGroup().getDisplayName()); |
| 131 | + System.out.println("* URN" + newCredentialGroup.getCredentialsGroup().getUrn()); |
| 132 | + |
| 133 | + /* list all available credentials groups */ |
| 134 | + System.out.println("\nListing all credentials groups:"); |
| 135 | + ListCredentialsGroupsResponse fetchedCredentialGroups = |
| 136 | + objectStorageApi.listCredentialsGroups(projectId, region); |
| 137 | + List<CredentialsGroup> listCredentialsGroups = |
| 138 | + fetchedCredentialGroups.getCredentialsGroups(); |
| 139 | + for (CredentialsGroup credentialsGroup : listCredentialsGroups) { |
| 140 | + System.out.println("*******************"); |
| 141 | + System.out.println("* Group ID: " + credentialsGroup.getCredentialsGroupId()); |
| 142 | + System.out.println("* Display name: " + credentialsGroup.getDisplayName()); |
| 143 | + System.out.println("* URN: " + credentialsGroup.getUrn()); |
| 144 | + } |
| 145 | + |
| 146 | + /* |
| 147 | + * /////////////////////////////////////////////////////// |
| 148 | + * // A C C E S S K E Y // |
| 149 | + * /////////////////////////////////////////////////////// |
| 150 | + * */ |
| 151 | + |
| 152 | + /* creating a new access key in the credentials group we just created */ |
| 153 | + System.out.println("\nCreating access key:"); |
| 154 | + CreateAccessKeyResponse newAccessKey = |
| 155 | + objectStorageApi.createAccessKey( |
| 156 | + projectId, |
| 157 | + region, |
| 158 | + new CreateAccessKeyPayload() |
| 159 | + .expires(OffsetDateTime.now().plusMinutes(5)), |
| 160 | + newCredentialGroup.getCredentialsGroup().getCredentialsGroupId()); |
| 161 | + System.out.println("* Key ID: " + newAccessKey.getKeyId()); |
| 162 | + System.out.println("* Display name: " + newAccessKey.getDisplayName()); |
| 163 | + System.out.println("* Expires: " + newAccessKey.getExpires()); |
| 164 | + |
| 165 | + /* list all available access key in the credential group */ |
| 166 | + System.out.println("\nListing all access key of the created credentials group:"); |
| 167 | + ListAccessKeysResponse fetchedAccessKeys = |
| 168 | + objectStorageApi.listAccessKeys( |
| 169 | + projectId, |
| 170 | + region, |
| 171 | + newCredentialGroup.getCredentialsGroup().getCredentialsGroupId()); |
| 172 | + List<AccessKey> listAccessKeys = fetchedAccessKeys.getAccessKeys(); |
| 173 | + for (AccessKey accessKey : listAccessKeys) { |
| 174 | + System.out.println("*******************"); |
| 175 | + System.out.println("* Key ID: " + accessKey.getKeyId()); |
| 176 | + System.out.println("* Display name: " + accessKey.getDisplayName()); |
| 177 | + System.out.println("* Expires: " + accessKey.getExpires()); |
| 178 | + } |
| 179 | + |
| 180 | + /* |
| 181 | + * /////////////////////////////////////////////////////// |
| 182 | + * // D E L E T I O N // |
| 183 | + * /////////////////////////////////////////////////////// |
| 184 | + * */ |
| 185 | + |
| 186 | + /* deleting the access key we just created */ |
| 187 | + System.out.println("\nDeleting access key:"); |
| 188 | + DeleteAccessKeyResponse deletedAccessKey = |
| 189 | + objectStorageApi.deleteAccessKey( |
| 190 | + projectId, |
| 191 | + region, |
| 192 | + newAccessKey.getKeyId(), |
| 193 | + newCredentialGroup.getCredentialsGroup().getCredentialsGroupId()); |
| 194 | + System.out.println("* Key ID: " + deletedAccessKey.getKeyId()); |
| 195 | + System.out.println("* Bucket successfully deleted"); |
| 196 | + |
| 197 | + /* deleting the credentials group we just created */ |
| 198 | + System.out.println("\nDeleting credentials group:"); |
| 199 | + DeleteCredentialsGroupResponse deleteCredentialsGroupResponse = |
| 200 | + objectStorageApi.deleteCredentialsGroup( |
| 201 | + projectId, |
| 202 | + region, |
| 203 | + newCredentialGroup.getCredentialsGroup().getCredentialsGroupId()); |
| 204 | + System.out.println( |
| 205 | + "* Group ID: " + deleteCredentialsGroupResponse.getCredentialsGroupId()); |
| 206 | + System.out.println("* Credentials group successfully deleted"); |
| 207 | + } catch (ApiException e) { |
| 208 | + throw new RuntimeException(e); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments