Skip to content

samples(Storage): Add samples and tests for ObjectContext#3328

Open
mahendra-google wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
mahendra-google:object-context-samples
Open

samples(Storage): Add samples and tests for ObjectContext#3328
mahendra-google wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
mahendra-google:object-context-samples

Conversation

@mahendra-google
Copy link
Copy Markdown
Contributor

@mahendra-google mahendra-google commented Mar 30, 2026

This PR adds three new samples and tests for Object Contexts.

storage_get_object_contexts : Retrieves the ContextsData for a specific object.

storage_list_object_contexts : Demonstrates how to iterate through and display the context metadata associated with an object.

storage_set_object_contexts: llustrates how to update or apply new context data to an existing object.

Note :- We need a new version of Google.Cloud.Storage.V1 Nuget Package which contains Filter option of ListObjectsOptions so that sample and test for List Objects with Context Data will not fail on Kokoro server. As of now I have updated Google.Cloud.Storage.V1 package version to 4.15.0 in the project file which is yet to be released.

@mahendra-google mahendra-google requested review from a team as code owners March 30, 2026 09:50
@snippet-bot
Copy link
Copy Markdown

snippet-bot bot commented Mar 30, 2026

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels Mar 30, 2026
Copy link
Copy Markdown

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

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 new sample code and unit tests for managing object contexts in Google Cloud Storage, including functionality to set, get, and list objects with context filters. Additionally, the Google.Cloud.Storage.V1 package is updated to version 4.15.0. The review feedback focuses on performance and readability improvements, specifically suggesting the removal of unnecessary list materialization when listing objects to reduce memory overhead, simplifying null-check logic using C# idioms, and clarifying documentation for filter string examples.

string filter = "contexts.\"KEY\"=\"VALUE\"")
{
var storage = StorageClient.Create();
var objects = storage.ListObjects(bucketName, prefix: null, new ListObjectsOptions { Filter = filter, Fields = "items(name,contexts)" }).ToList();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Calling .ToList() materializes the entire collection of objects in memory. For buckets with a large number of objects, this can lead to high memory consumption and performance issues. Since the method returns an IEnumerable<Object> and the subsequent code iterates over it, you can remove .ToList() to take advantage of deferred execution and stream the results from the API. This will make the sample more efficient and scalable.

        var objects = storage.ListObjects(bucketName, prefix: null, new ListObjectsOptions { Filter = filter, Fields = "items(name,contexts)" });

var obj = storage.GetObject(bucketName, objectName);
Google.Apis.Storage.v1.Data.Object.ContextsData contextsData = obj.Contexts;

if (contextsData?.Custom == null || contextsData.Custom.Count == 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The condition to check if contextsData.Custom is null or empty can be written more concisely using the null-conditional (?.) and null-coalescing (??) operators. This improves readability and is a common C# idiom.

        if ((contextsData?.Custom?.Count ?? 0) == 0)

/// List objects in the specified bucket that match a context filter.
/// </summary>
/// <param name="bucketName">The name of the bucket.</param>
/// <param name="filter">The metadata context filter (e.g. "contexts.\"KEY\":*", "-contexts.\"KEY\":*", "contexts.\"KEY\"=\"VALUE\"", "-contexts.\"KEY\"=\"VALUE\"").</param>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The example filter strings in the documentation contain unnecessary backslashes and are wrapped in quotes, which can be confusing. To improve clarity, please remove the backslashes and the outer quotes from the examples.

    /// <param name="filter">The metadata context filter (e.g. contexts."KEY":*, -contexts."KEY":*, contexts."KEY"="VALUE", -contexts."KEY"="VALUE").</param>

@bajajneha27 bajajneha27 removed their assignment Mar 31, 2026
Comment thread storage/api/Storage.Samples/SetObjectContexts.cs Outdated
@krishnamd-jkp krishnamd-jkp added the kokoro:run Add this label to force Kokoro to re-run the tests. label Apr 6, 2026
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants