Skip to content

feat(storage): add delete object contexts sample - #14794

Draft
sahusneha2004 wants to merge 1 commit into
mainfrom
storage-object-contexts-delete-sample-java
Draft

sahusneha2004 wants to merge 1 commit into
mainfrom
storage-object-contexts-delete-sample-java

Conversation

@sahusneha2004

Copy link
Copy Markdown

Adds a DeleteObjectContexts sample (region tag storage_delete_object_contexts) for java-storage that removes all custom object contexts from an existing object, plus an integration test (ITObjectSnippets#testDeleteObjectContexts).

This completes the object-contexts sample set for Java (set / get / list already exist; delete-all was previously only shown as a comment inside SetObjectContexts.java).

What it does

  1. Fetches the object (storage.get(BlobId)).
  2. Builds ObjectContexts with an empty custom map and applies it via blob.toBuilder().setContexts(...).
  3. Calls storage.update(..., BlobTargetOption.generationMatch()) (same precondition style as SetObjectContexts).
  4. Prints Deleted all custom contexts for object <object> in bucket <bucket>.

SDK mechanism note (verified in library source)

ObjectContexts.Builder.setCustom(null) is normalized to ImmutableMap.of() (BlobInfo.java), so an empty map and null are equivalent. BlobInfo.BuilderImpl.setContexts diffs the new map against the fetched blob's contexts (Utils.diffMaps) and, when the new map is empty, records a per-key modified field contexts.custom.<key> for every existing key. These are sent as deletions over JSON ({"contexts":{"custom":{"<key>":null,...}}}) and as update_mask paths over gRPC. The library's own ITNestedUpdateMaskTest ("1 key, set empty") covers this on both transports. The sample uses the explicit empty map for clarity, and the builder is derived from the fetched blob (required for the diff to produce deletions).

Test

ITObjectSnippets#testDeleteObjectContexts: uploads an object, sets a context via SetObjectContexts, asserts it is present, runs DeleteObjectContexts, asserts stdout contains Deleted all custom contexts for object <name> and that the re-read object's contexts are null/empty.

Local validation not run (mvn/java unavailable in the agent sandbox); relying on CI for fmt/checkstyle and samples ITs.

Internal bug: b/482946642

[Generated-by: AI]

Adds DeleteObjectContexts sample (region tag storage_delete_object_contexts) that clears all custom contexts on an object with a precondition, plus an integration test.

Internal bug: b/482946642

[Generated-by: AI]
@sahusneha2004 sahusneha2004 added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. ai-generated storage-sample-architect Generated by storage-sample-architect skill labels Oct 27, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces the DeleteObjectContexts class to delete all custom contexts of a Google Cloud Storage object by setting them to an empty map, and adds a corresponding integration test. The feedback recommends replacing JUnit's assertTrue with Google Truth's assertThat in the test assertion to ensure consistency with the rest of the test suite.


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();
    }

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. storage-sample-architect Generated by storage-sample-architect skill

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant