diff --git a/storage/src/get_object_contexts.php b/storage/src/get_object_contexts.php new file mode 100644 index 000000000..35a4a61e7 --- /dev/null +++ b/storage/src/get_object_contexts.php @@ -0,0 +1,57 @@ +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); diff --git a/storage/src/list_object_contexts.php b/storage/src/list_object_contexts.php new file mode 100644 index 000000000..58a82d8b8 --- /dev/null +++ b/storage/src/list_object_contexts.php @@ -0,0 +1,52 @@ +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); diff --git a/storage/src/set_object_contexts.php b/storage/src/set_object_contexts.php new file mode 100644 index 000000000..7f341cd87 --- /dev/null +++ b/storage/src/set_object_contexts.php @@ -0,0 +1,64 @@ +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); diff --git a/storage/test/ObjectsTest.php b/storage/test/ObjectsTest.php index f88af49e0..0f94f0b17 100644 --- a/storage/test/ObjectsTest.php +++ b/storage/test/ObjectsTest.php @@ -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(); + } + public function testListObjects() { $output = self::runFunctionSnippet('list_objects', [