samples(Storage): Add samples and tests for ObjectContext#3328
samples(Storage): Add samples and tests for ObjectContext#3328mahendra-google wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
Conversation
|
Here is the summary of changes. You are about to add 3 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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) |
| /// 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> |
There was a problem hiding this comment.
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>
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.V1Nuget Package which containsFilteroption ofListObjectsOptionsso that sample and test forList Objects with Context Datawill not fail on Kokoro server. As of now I have updatedGoogle.Cloud.Storage.V1package version to4.15.0in the project file which is yet to be released.