-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(storage): add Object Contexts samples and system tests #2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
salilg-eng
wants to merge
8
commits into
GoogleCloudPlatform:main
Choose a base branch
from
salilg-eng:feat/sample-object-contexts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10f5d6f
Object Contexts Sample
salilg-eng 3083a83
Final code
salilg-eng d1c784b
Final code
salilg-eng 9cf1530
Final code
salilg-eng e061129
Final code
salilg-eng 7b2aeac
Comments resolved
salilg-eng 60200fd
Update object contexts
salilg-eng 6553999
Fixes as per gemini review
salilg-eng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_get_object_contexts] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * Retrieve contexts for an existing object. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $objectName The name of your Cloud Storage object. | ||
| * (e.g. 'my-object') | ||
| */ | ||
| function get_object_contexts(string $bucketName, string $objectName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
| $object = $bucket->object($objectName); | ||
|
|
||
| $info = $object->info(); | ||
| if (isset($info['contexts']['custom'])) { | ||
| printf('Contexts for object %s:' . PHP_EOL, $objectName); | ||
| foreach ($info['contexts']['custom'] as $key => $data) { | ||
| printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value'] ?? ''); | ||
| printf(' - Created: %s' . PHP_EOL, $data['createTime'] ?? ''); | ||
| printf(' - Updated: %s' . PHP_EOL, $data['updateTime'] ?? ''); | ||
| } | ||
| } | ||
| } | ||
| # [END storage_get_object_contexts] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_list_object_contexts] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * List objects with a filter based on contexts. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| */ | ||
| function list_object_contexts(string $bucketName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
|
|
||
| // Example filter: find objects where department is finance | ||
| $options = [ | ||
| 'filter' => 'contexts."department"="finance"' | ||
| ]; | ||
| foreach ($bucket->objects($options) as $object) { | ||
| printf('Found object: %s' . PHP_EOL, $object->name()); | ||
| } | ||
| } | ||
| # [END storage_list_object_contexts] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_set_object_contexts] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * Attach or modify contexts to an existing object. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $objectName The name of your Cloud Storage object. | ||
| * (e.g. 'my-object') | ||
| */ | ||
| function set_object_contexts(string $bucketName, string $objectName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
| $object = $bucket->object($objectName); | ||
|
|
||
| // Contexts are defined under the 'custom' key; keys/values must start with an alphanumeric and avoid quotes/slashes. | ||
| $object->update([ | ||
| 'contexts' => [ | ||
| 'custom' => [ | ||
| 'department' => ['value' => 'finance'], | ||
| 'priority' => ['value' => 'high'] | ||
| ] | ||
| ] | ||
| ]); | ||
| $info = $object->info(); | ||
| if (isset($info['contexts']['custom'])) { | ||
| printf('Contexts for object %s were updated:' . PHP_EOL, $objectName); | ||
| foreach ($info['contexts']['custom'] as $key => $data) { | ||
| printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value']); | ||
| } | ||
| } | ||
| } | ||
| # [END storage_set_object_contexts] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,69 @@ public static function setUpBeforeClass(): void | |
| self::$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'; | ||
| } | ||
|
|
||
| public function testObjectContexts() | ||
| { | ||
| $objectContextsBucket = self::$storage->createBucket( | ||
| sprintf('%s-test-bucket-%s', self::$projectId, uniqid()) | ||
| ); | ||
| $bucketName = $objectContextsBucket->name(); | ||
| $objectName = 'object-contexts-'.uniqid(); | ||
| $object = $objectContextsBucket->upload('test', [ | ||
| 'name' => $objectName, | ||
| ]); | ||
| $objectInfo = $object->info()['name']; | ||
|
|
||
| // Set Object Contexts | ||
| $setOutput = $this->runFunctionSnippet('set_object_contexts', [ | ||
| $bucketName, | ||
| $objectInfo, | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString("Contexts for object $objectInfo were updated", $setOutput); | ||
|
|
||
| // Get Object Contexts | ||
| $getOutput = $this->runFunctionSnippet('get_object_contexts', [ | ||
| $bucketName, | ||
| $objectInfo | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('Key: department, Value: finance', $getOutput); | ||
| $this->assertStringContainsString('Key: priority, Value: high', $getOutput); | ||
|
|
||
| // List Object Contexts using filter | ||
| $listOutput = $this->runFunctionSnippet('list_object_contexts', [ | ||
| $bucketName | ||
| ]); | ||
| $this->assertStringContainsString("Found object: $objectInfo", $listOutput); | ||
|
|
||
| // Clear all contexts on the object. | ||
| $object->update([ | ||
| 'contexts' => [ | ||
| 'custom' => null, | ||
| ], | ||
| ]); | ||
| $object->reload(); | ||
|
|
||
| $clearedGetOutput = $this->runFunctionSnippet('get_object_contexts', [ | ||
| $bucketName, | ||
| $objectName | ||
| ]); | ||
|
|
||
| $this->assertStringNotContainsString('Key: department, Value: finance', $clearedGetOutput); | ||
| $this->assertStringNotContainsString('Key: priority, Value: high', $clearedGetOutput); | ||
|
|
||
| $clearedInfo = $object->info(); | ||
| $this->assertTrue(empty($clearedInfo['contexts']['custom'] ?? [])); | ||
|
|
||
| // List again with filter after clearing contexts. | ||
| $clearedListOutput = $this->runFunctionSnippet('list_object_contexts', [ | ||
| $bucketName | ||
| ]); | ||
| $this->assertEmpty(trim($clearedListOutput)); | ||
| $object->delete(); | ||
| $objectContextsBucket->delete(); | ||
| } | ||
|
Comment on lines
+47
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes both object and bucket deleted |
||
|
|
||
| public function testListObjects() | ||
| { | ||
| $output = self::runFunctionSnippet('list_objects', [ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description mentions implementing comprehensive system tests covering presence, absence (
-), and existence (:) filter operators instorageTest.php. However, these tests are missing from the current changes inObjectsTest.php. Please include the promised test cases to ensure full coverage of the filtering logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is my bad above description mentioned storageTest.php actually made changes in ObjectTest.php file.