|
| 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