Skip to content

Commit 5cd310f

Browse files
authored
feat(storage): add Object Contexts samples and system tests (#2200)
1 parent ecb2a66 commit 5cd310f

4 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_object_contexts]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Retrieve contexts for an existing object.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
* @param string $objectName The name of your Cloud Storage object.
35+
* (e.g. 'my-object')
36+
*/
37+
function get_object_contexts(string $bucketName, string $objectName): void
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$object = $bucket->object($objectName);
42+
43+
$info = $object->info();
44+
if (isset($info['contexts']['custom'])) {
45+
printf('Contexts for object %s:' . PHP_EOL, $objectName);
46+
foreach ($info['contexts']['custom'] as $key => $data) {
47+
printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value'] ?? '');
48+
printf(' - Created: %s' . PHP_EOL, $data['createTime'] ?? '');
49+
printf(' - Updated: %s' . PHP_EOL, $data['updateTime'] ?? '');
50+
}
51+
}
52+
}
53+
# [END storage_get_object_contexts]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_list_object_contexts]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* List objects with a filter based on contexts.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function list_object_contexts(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
// Examples of filters based on contexts:
41+
// 1. Objects with a specific key attached: 'contexts:"department"'
42+
// 2. Objects WITHOUT a specific key attached: '-contexts:"department"'
43+
// 3. Objects with a specific key-value pair: 'contexts."department"="finance"'
44+
// 4. Objects WITHOUT a specific key-value pair: '-contexts."department"="finance"'
45+
$options = [
46+
'filter' => 'contexts."department"="finance"'
47+
];
48+
foreach ($bucket->objects($options) as $object) {
49+
printf('Found object: %s' . PHP_EOL, $object->name());
50+
}
51+
}
52+
# [END storage_list_object_contexts]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_set_object_contexts]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Attach or modify contexts to an existing object.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
* @param string $objectName The name of your Cloud Storage object.
35+
* (e.g. 'my-object')
36+
*/
37+
function set_object_contexts(string $bucketName, string $objectName): void
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$object = $bucket->object($objectName);
42+
43+
// Contexts are defined under the 'custom' key; keys/values must start with an alphanumeric and avoid quotes/slashes.
44+
$object->update([
45+
'contexts' => [
46+
'custom' => [
47+
'department' => ['value' => 'finance'],
48+
'priority' => ['value' => 'high']
49+
]
50+
]
51+
]);
52+
$info = $object->info();
53+
if (isset($info['contexts']['custom'])) {
54+
printf('Contexts for object %s were updated:' . PHP_EOL, $objectName);
55+
foreach ($info['contexts']['custom'] as $key => $data) {
56+
printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value']);
57+
}
58+
}
59+
}
60+
# [END storage_set_object_contexts]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

‎storage/test/ObjectsTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,69 @@ public static function setUpBeforeClass(): void
4242
self::$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
4343
}
4444

45+
public function testObjectContexts()
46+
{
47+
$objectContextsBucket = self::$storage->createBucket(
48+
sprintf('%s-test-bucket-%s', self::$projectId, uniqid())
49+
);
50+
$bucketName = $objectContextsBucket->name();
51+
$objectName = 'object-contexts-' . uniqid();
52+
$object = $objectContextsBucket->upload('test', [
53+
'name' => $objectName,
54+
]);
55+
$objectInfo = $object->info()['name'];
56+
57+
// Set Object Contexts
58+
$setOutput = $this->runFunctionSnippet('set_object_contexts', [
59+
$bucketName,
60+
$objectInfo,
61+
]);
62+
63+
$this->assertStringContainsString("Contexts for object $objectInfo were updated", $setOutput);
64+
65+
// Get Object Contexts
66+
$getOutput = $this->runFunctionSnippet('get_object_contexts', [
67+
$bucketName,
68+
$objectInfo
69+
]);
70+
71+
$this->assertStringContainsString('Key: department, Value: finance', $getOutput);
72+
$this->assertStringContainsString('Key: priority, Value: high', $getOutput);
73+
74+
// List Object Contexts using filter
75+
$listOutput = $this->runFunctionSnippet('list_object_contexts', [
76+
$bucketName
77+
]);
78+
$this->assertStringContainsString("Found object: $objectInfo", $listOutput);
79+
80+
// Clear all contexts on the object.
81+
$object->update([
82+
'contexts' => [
83+
'custom' => null,
84+
],
85+
]);
86+
$object->reload();
87+
88+
$clearedGetOutput = $this->runFunctionSnippet('get_object_contexts', [
89+
$bucketName,
90+
$objectName
91+
]);
92+
93+
$this->assertStringNotContainsString('Key: department, Value: finance', $clearedGetOutput);
94+
$this->assertStringNotContainsString('Key: priority, Value: high', $clearedGetOutput);
95+
96+
$clearedInfo = $object->info();
97+
$this->assertTrue(empty($clearedInfo['contexts']['custom'] ?? []));
98+
99+
// List again with filter after clearing contexts.
100+
$clearedListOutput = $this->runFunctionSnippet('list_object_contexts', [
101+
$bucketName
102+
]);
103+
$this->assertEmpty(trim($clearedListOutput));
104+
$object->delete();
105+
$objectContextsBucket->delete();
106+
}
107+
45108
public function testListObjects()
46109
{
47110
$output = self::runFunctionSnippet('list_objects', [

0 commit comments

Comments
 (0)