Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.storage.object;

// [START storage_delete_object_contexts]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BlobInfo.ObjectContexts;
import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Maps;
import java.util.Map;

public class DeleteObjectContexts {
public static void deleteObjectContexts(String projectId, String bucketName, String objectName)
throws Exception {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String objectName = "your-object-name";

try (Storage storage =
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
BlobId blobId = BlobId.of(bucketName, objectName);
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// Recommended: Set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to update returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

// To delete all existing custom contexts, set the custom contexts to an empty map.
// The update is computed against the contexts of the fetched object, so every
// existing key is removed.
Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();
ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();

BlobInfo pendingUpdate = blob.toBuilder().setContexts(contexts).build();
storage.update(pendingUpdate, precondition);

System.out.println(
"Deleted all custom contexts for object " + objectName + " in bucket " + bucketName);
}
}
}
// [END storage_delete_object_contexts]
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.example.storage.object.CopyObject;
import com.example.storage.object.CopyOldVersionOfObject;
import com.example.storage.object.DeleteObject;
import com.example.storage.object.DeleteObjectContexts;
import com.example.storage.object.DeleteOldVersionOfObject;
import com.example.storage.object.DownloadEncryptedObject;
import com.example.storage.object.DownloadObject;
Expand Down Expand Up @@ -685,6 +686,28 @@ public void testSetObjectContexts() throws Exception {
assertThat(updatedBlob.getContexts().getCustom().get(key).getValue()).isEqualTo(value);
}

@Test
public void testDeleteObjectContexts() throws Exception {
String blobName = generator.randomObjectName();
String key = "test-key-delete";
String value = "test-value-delete";

storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist());

SetObjectContexts.setObjectContexts(
GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName, key, value);
Blob blobWithContexts = storage.get(bucket.getName(), blobName);
assertThat(blobWithContexts.getContexts().getCustom()).containsKey(key);

DeleteObjectContexts.deleteObjectContexts(GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName);
String deleteOutput = stdOut.getCapturedOutputAsUtf8String();
assertThat(deleteOutput).contains("Deleted all custom contexts for object " + blobName);

Blob updatedBlob = storage.get(bucket.getName(), blobName);
ObjectContexts contexts = updatedBlob.getContexts();
assertTrue(contexts == null || contexts.getCustom().isEmpty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the rest of the test suite which uses Google Truth (assertThat), avoid using JUnit's assertTrue. You can assert that the custom contexts are empty if they are not null.

    if (contexts != null) {
      assertThat(contexts.getCustom()).isEmpty();
    }

}

@Test
public void testGetObjectContexts() throws Exception {
String blobName = generator.randomObjectName();
Expand Down
Loading