Skip to content

Commit f8cfafa

Browse files
committed
feat(objectstorage): onboard objectstorage
- add examples - add changelog entries
1 parent 41b7f3c commit f8cfafa

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Release (2025-MM-DD)
2+
- `objectstorage`: [v0.1.0](services/objectstorage/CHANGELOG.md#v010)
3+
- Initial onboarding of STACKIT Java SDK for Object storage service
4+
15
## Release (2025-10-29)
26
- `core`:
37
- [v0.4.0](core/CHANGELOG.md#v040)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies {
2+
implementation project (':services:objectstorage')
3+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
4+
}
5+
6+
ext.mainClassName = 'cloud.stackit.sdk.objectstorage.examples.ObjectStorageExample'
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
.readTimeout(Duration.ofSeconds(15))
40+
.build();
41+
42+
ObjectStorageApi objectStorageApi = new ObjectStorageApi(httpClient);
43+
44+
// the id of your STACKIT project, read from env var for this example
45+
String projectId = System.getenv("STACKIT_PROJECT_ID");
46+
if (projectId == null || projectId.isEmpty()) {
47+
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
48+
return;
49+
}
50+
51+
// the region which should be used to interact with objectstorage, read from env var for
52+
// this example
53+
String region = System.getenv("STACKIT_REGION");
54+
if (region == null || region.isEmpty()) {
55+
System.err.println("Environment variable 'STACKIT_REGION' not found.");
56+
return;
57+
}
58+
59+
try {
60+
/*
61+
* ///////////////////////////////////////////////////////
62+
* // P R O J E C T E N A B L E M E N T //
63+
* ///////////////////////////////////////////////////////
64+
*/
65+
/* check if object storage is enabled for the project */
66+
ProjectStatus projectStatus;
67+
System.out.println("Checking if object storage is enabled");
68+
try {
69+
projectStatus = objectStorageApi.getServiceStatus(projectId, region);
70+
System.out.println("* Object storage is enabled");
71+
} catch (ApiException e) {
72+
// If response status is not found, object storage is not enabled for the
73+
// project
74+
if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) {
75+
System.out.println("* Object storage is not enabled for the project");
76+
System.out.println("* Enabling object storage");
77+
projectStatus = objectStorageApi.enableService(projectId, region);
78+
System.out.println("* Object storage successful enabled for the project");
79+
} else {
80+
throw new RuntimeException(e);
81+
}
82+
}
83+
ProjectScope scope = projectStatus.getScope();
84+
System.out.println("* Scope of the object storage: " + scope);
85+
86+
/*
87+
* ///////////////////////////////////////////////////////
88+
* // B U C K E T S //
89+
* ///////////////////////////////////////////////////////
90+
* */
91+
92+
/* create a new bucket in the project */
93+
System.out.println("\nCreating bucket");
94+
CreateBucketResponse newBucket =
95+
objectStorageApi.createBucket(projectId, region, "java-sdk-example");
96+
System.out.println(" * Bucket name: " + newBucket.getBucket());
97+
System.out.println(" * Project ID: " + newBucket.getProject());
98+
99+
/* list all available buckets in the project */
100+
System.out.println("\nListing all buckets:");
101+
ListBucketsResponse fetchedBuckets = objectStorageApi.listBuckets(projectId, region);
102+
List<Bucket> listBuckets = fetchedBuckets.getBuckets();
103+
for (Bucket bucket : listBuckets) {
104+
System.out.println("*******************");
105+
System.out.println("* Bucket name: " + bucket.getName());
106+
System.out.println("* Virtual host: " + bucket.getUrlPathStyle());
107+
}
108+
109+
/* deleting the bucket we just created */
110+
System.out.println("\nDeleting bucket:");
111+
DeleteBucketResponse deleteBucketResponse =
112+
objectStorageApi.deleteBucket(projectId, region, newBucket.getBucket());
113+
System.out.println("* Bucket name: " + deleteBucketResponse.getBucket());
114+
System.out.println("* Bucket successfully deleted");
115+
116+
/*
117+
* ///////////////////////////////////////////////////////
118+
* // C R E D E N T I A L G R O U P //
119+
* ///////////////////////////////////////////////////////
120+
* */
121+
122+
/* creating a new credential group in the project */
123+
System.out.println("\nCreating credential group:");
124+
CreateCredentialsGroupResponse newCredentialGroup =
125+
objectStorageApi.createCredentialsGroup(
126+
projectId,
127+
region,
128+
new CreateCredentialsGroupPayload().displayName("java-sdk-example"));
129+
System.out.println(
130+
"* Group ID"
131+
+ newCredentialGroup.getCredentialsGroup().getCredentialsGroupId());
132+
System.out.println(
133+
"* Display name" + newCredentialGroup.getCredentialsGroup().getDisplayName());
134+
System.out.println("* URN" + newCredentialGroup.getCredentialsGroup().getUrn());
135+
136+
/* list all available credentials groups */
137+
System.out.println("\nListing all credentials groups:");
138+
ListCredentialsGroupsResponse fetchedCredentialGroups =
139+
objectStorageApi.listCredentialsGroups(projectId, region);
140+
List<CredentialsGroup> listCredentialsGroups =
141+
fetchedCredentialGroups.getCredentialsGroups();
142+
for (CredentialsGroup credentialsGroup : listCredentialsGroups) {
143+
System.out.println("*******************");
144+
System.out.println("* Group ID: " + credentialsGroup.getCredentialsGroupId());
145+
System.out.println("* Display name: " + credentialsGroup.getDisplayName());
146+
System.out.println("* URN: " + credentialsGroup.getUrn());
147+
}
148+
149+
/*
150+
* ///////////////////////////////////////////////////////
151+
* // A C C E S S K E Y //
152+
* ///////////////////////////////////////////////////////
153+
* */
154+
155+
/* creating a new access key in the credentials group we just created */
156+
System.out.println("\nCreating access key:");
157+
CreateAccessKeyResponse newAccessKey =
158+
objectStorageApi.createAccessKey(
159+
projectId,
160+
region,
161+
new CreateAccessKeyPayload()
162+
.expires(OffsetDateTime.now().plusMinutes(5)),
163+
newCredentialGroup.getCredentialsGroup().getCredentialsGroupId());
164+
System.out.println("* Key ID: " + newAccessKey.getKeyId());
165+
System.out.println("* Display name: " + newAccessKey.getDisplayName());
166+
System.out.println("* Expires: " + newAccessKey.getExpires());
167+
168+
/* list all available access key in the credential group */
169+
System.out.println("\nListing all access key of the created credentials group:");
170+
ListAccessKeysResponse fetchedAccessKeys =
171+
objectStorageApi.listAccessKeys(
172+
projectId,
173+
region,
174+
newCredentialGroup.getCredentialsGroup().getCredentialsGroupId());
175+
List<AccessKey> listAccessKeys = fetchedAccessKeys.getAccessKeys();
176+
for (AccessKey accessKey : listAccessKeys) {
177+
System.out.println("*******************");
178+
System.out.println("* Key ID: " + accessKey.getKeyId());
179+
System.out.println("* Display name: " + accessKey.getDisplayName());
180+
System.out.println("* Expires: " + accessKey.getExpires());
181+
}
182+
183+
/*
184+
* ///////////////////////////////////////////////////////
185+
* // D E L E T I O N //
186+
* ///////////////////////////////////////////////////////
187+
* */
188+
189+
/* deleting the access key we just created */
190+
System.out.println("\nDeleting access key:");
191+
DeleteAccessKeyResponse deletedAccessKey =
192+
objectStorageApi.deleteAccessKey(
193+
projectId,
194+
region,
195+
newAccessKey.getKeyId(),
196+
newCredentialGroup.getCredentialsGroup().getCredentialsGroupId());
197+
System.out.println("* Key ID: " + deletedAccessKey.getKeyId());
198+
System.out.println("* Bucket successfully deleted");
199+
200+
/* deleting the credentials group we just created */
201+
System.out.println("\nDeleting credentials group:");
202+
DeleteCredentialsGroupResponse deleteCredentialsGroupResponse =
203+
objectStorageApi.deleteCredentialsGroup(
204+
projectId,
205+
region,
206+
newCredentialGroup.getCredentialsGroup().getCredentialsGroupId());
207+
System.out.println(
208+
"* Group ID: " + deleteCredentialsGroupResponse.getCredentialsGroupId());
209+
System.out.println("* Bucket successfully deleted");
210+
} catch (ApiException e) {
211+
throw new RuntimeException(e);
212+
}
213+
}
214+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## v0.1.0
2+
- Initial onboarding of STACKIT Java SDK for Object storage service

services/objectstorage/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

0 commit comments

Comments
 (0)