Skip to content

Commit 9dcce11

Browse files
authored
samples: add samples for object contexts (#3329)
1 parent 7e42c2f commit 9dcce11

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_get_object_contexts]
20+
21+
import com.google.cloud.storage.Blob;
22+
import com.google.cloud.storage.BlobInfo.ObjectContexts;
23+
import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import java.util.Map;
27+
28+
public class GetObjectContexts {
29+
public static void getObjectContexts(String projectId, String bucketName, String objectName)
30+
throws Exception {
31+
// The ID of your GCP project
32+
// String projectId = "your-project-id";
33+
34+
// The ID of your GCS bucket
35+
// String bucketName = "your-unique-bucket-name";
36+
37+
// The ID of your GCS object
38+
// String objectName = "your-object-name";
39+
40+
try (Storage storage =
41+
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
42+
43+
Blob blob = storage.get(bucketName, objectName);
44+
if (blob == null) {
45+
System.out.println("The object " + objectName + " was not found in " + bucketName);
46+
return;
47+
}
48+
ObjectContexts objectContexts = blob.getContexts();
49+
50+
if (objectContexts != null) {
51+
Map<String, ObjectCustomContextPayload> customContexts = objectContexts.getCustom();
52+
if (customContexts == null) {
53+
System.out.println("No custom contexts found for object: " + objectName);
54+
return;
55+
}
56+
// Print blob's object contexts
57+
System.out.println("\nCustom Contexts:");
58+
for (Map.Entry<String, ObjectCustomContextPayload> custom : customContexts.entrySet()) {
59+
System.out.println(custom.getKey() + "=" + custom.getValue());
60+
}
61+
}
62+
}
63+
}
64+
}
65+
// [END storage_get_object_contexts]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_list_object_contexts]
20+
21+
import com.google.api.gax.paging.Page;
22+
import com.google.cloud.storage.Blob;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
26+
public class ListObjectContexts {
27+
public static void listObjectContexts(String projectId, String bucketName, String key)
28+
throws Exception {
29+
// The ID of your GCP project
30+
// String projectId = "your-project-id";
31+
32+
// The ID of your GCS bucket
33+
// String bucketName = "your-unique-bucket-name";
34+
35+
// The context key you want to filter
36+
// String key = "your-context-key";
37+
38+
try (Storage storage =
39+
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
40+
/*
41+
* List any object that has a context with the specified key attached
42+
* String filter = "contexts.\"KEY\":*";
43+
*
44+
* List any object that that does not have a context with the specified key attached
45+
* String filter = "NOT contexts.\"KEY\":*";
46+
*
47+
* List any object that has a context with the specified key and value attached
48+
* String filter = "contexts.\"KEY\"=\"VALUE\"";
49+
*
50+
* List any object that does not have a context with the specified key and value attached
51+
* String filter = "NOT contexts.\"KEY\"=\"VALUE\"";
52+
*/
53+
54+
String filter = "contexts.\"" + key + "\":*";
55+
56+
System.out.println("Listing objects for bucket: " + bucketName + "with context key: " + key);
57+
Page<Blob> blobs = storage.list(bucketName, Storage.BlobListOption.filter(filter));
58+
for (Blob blob : blobs.iterateAll()) {
59+
System.out.println(blob.getBlobId().toGsUtilUri());
60+
}
61+
}
62+
}
63+
}
64+
// [END storage_list_object_contexts]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_set_object_contexts]
20+
21+
import com.google.cloud.storage.Blob;
22+
import com.google.cloud.storage.BlobId;
23+
import com.google.cloud.storage.BlobInfo;
24+
import com.google.cloud.storage.BlobInfo.ObjectContexts;
25+
import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;
26+
import com.google.cloud.storage.Storage;
27+
import com.google.cloud.storage.StorageOptions;
28+
import com.google.common.collect.Maps;
29+
import java.util.Map;
30+
31+
public class SetObjectContexts {
32+
public static void setObjectContexts(
33+
String projectId, String bucketName, String objectName, String key, String value)
34+
throws Exception {
35+
// The ID of your GCP project
36+
// String projectId = "your-project-id";
37+
38+
// The ID of your GCS bucket
39+
// String bucketName = "your-unique-bucket-name";
40+
41+
// The ID of your GCS object
42+
// String objectName = "your-object-name";
43+
44+
// The context key-value you want to add
45+
// String key = "your-context-key";
46+
// String value = "your-context-value";
47+
48+
try (Storage storage =
49+
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
50+
BlobId blobId = BlobId.of(bucketName, objectName);
51+
Blob blob = storage.get(blobId);
52+
if (blob == null) {
53+
System.out.println("The object " + objectName + " was not found in " + bucketName);
54+
return;
55+
}
56+
57+
// Recommended: Set a generation-match precondition to avoid potential race
58+
// conditions and data corruptions. The request to update returns a 412 error if
59+
// the object's generation number does not match your precondition.
60+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
61+
62+
// This section demonstrates how to upsert, delete all, and delete a specific context.
63+
64+
// To upsert a context (if the key already exists, its value is replaced;
65+
// otherwise, a new key-value pair is added):
66+
ObjectCustomContextPayload payload =
67+
ObjectCustomContextPayload.newBuilder().setValue(value).build();
68+
Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();
69+
custom.put(key, payload);
70+
ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();
71+
72+
/*
73+
* To delete all existing contexts:
74+
* ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(null).build();
75+
*/
76+
77+
/*
78+
* To delete a specific key from the context:
79+
* Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();
80+
* custom.put(key, null);
81+
* ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();
82+
*/
83+
BlobInfo pendingUpdate = blob.toBuilder().setContexts(contexts).build();
84+
storage.update(pendingUpdate, precondition);
85+
86+
System.out.println(
87+
"Updated custom contexts for object " + objectName + " in bucket " + bucketName);
88+
}
89+
}
90+
}
91+
// [END storage_set_object_contexts]

0 commit comments

Comments
 (0)