Skip to content

Commit 576286e

Browse files
authored
feat(Storage): object get kms key (GoogleCloudPlatform#1695)
1 parent c6dd242 commit 576286e

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

storage/src/object_get_kms_key.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
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/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_object_get_kms_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Retrieve the KMS key of an 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 object in the bucket.
35+
* (e.g. 'my-object')
36+
*/
37+
function object_get_kms_key(string $bucketName, string $objectName): void
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$object = $bucket->object($objectName);
42+
$info = $object->info();
43+
44+
printf(
45+
'The KMS key of the object is %s' . PHP_EOL,
46+
$info['kmsKeyName'],
47+
);
48+
}
49+
# [END storage_object_get_kms_key]
50+
51+
// The following 2 lines are only needed to run the samples
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/storageTest.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function testPrintDefaultBucketAcl()
7171
$defaultAcl = self::$tempBucket->defaultAcl()->get();
7272
foreach ($defaultAcl as $item) {
7373
$this->assertStringContainsString(
74-
sprintf('%s: %s' . PHP_EOL, $item['entity'], $item['role']),
75-
$output,
74+
sprintf('%s: %s' . PHP_EOL, $item['entity'], $item['role']),
75+
$output,
7676
);
7777
}
7878
}
@@ -475,8 +475,30 @@ public function testUploadWithKmsKey()
475475
$objectName,
476476
$this->keyName()
477477
));
478+
479+
return $objectName;
478480
}
479481

482+
/** @depends testUploadWithKmsKey */
483+
public function testObjectGetKmsKey(string $objectName)
484+
{
485+
$kmsEncryptedBucketName = self::$bucketName . '-kms-encrypted';
486+
$bucket = self::$storage->bucket($kmsEncryptedBucketName);
487+
$objectInfo = $bucket->object($objectName)->info();
488+
489+
$output = $this->runFunctionSnippet('object_get_kms_key', [
490+
$kmsEncryptedBucketName,
491+
$objectName,
492+
]);
493+
494+
$this->assertEquals(
495+
sprintf(
496+
'The KMS key of the object is %s' . PHP_EOL,
497+
$objectInfo['kmsKeyName'],
498+
),
499+
$output,
500+
);
501+
}
480502
public function testBucketVersioning()
481503
{
482504
$output = self::runFunctionSnippet('enable_versioning', [
@@ -510,8 +532,8 @@ public function testBucketWebsiteConfiguration()
510532
]);
511533

512534
$this->assertEquals(
513-
sprintf('Bucket website configuration not set' . PHP_EOL),
514-
$output,
535+
sprintf('Bucket website configuration not set' . PHP_EOL),
536+
$output,
515537
);
516538

517539
$output = self::runFunctionSnippet('define_bucket_website_configuration', [
@@ -521,13 +543,13 @@ public function testBucketWebsiteConfiguration()
521543
]);
522544

523545
$this->assertEquals(
524-
sprintf(
525-
'Static website bucket %s is set up to use %s as the index page and %s as the 404 page.',
526-
$bucket->name(),
527-
$obj->name(),
528-
$obj->name(),
529-
),
530-
$output
546+
sprintf(
547+
'Static website bucket %s is set up to use %s as the index page and %s as the 404 page.',
548+
$bucket->name(),
549+
$obj->name(),
550+
$obj->name(),
551+
),
552+
$output
531553
);
532554

533555
$info = $bucket->reload();
@@ -537,12 +559,12 @@ public function testBucketWebsiteConfiguration()
537559
]);
538560

539561
$this->assertEquals(
540-
sprintf(
541-
'Index page: %s' . PHP_EOL . '404 page: %s' . PHP_EOL,
542-
$info['website']['mainPageSuffix'],
543-
$info['website']['notFoundPage'],
544-
),
545-
$output,
562+
sprintf(
563+
'Index page: %s' . PHP_EOL . '404 page: %s' . PHP_EOL,
564+
$info['website']['mainPageSuffix'],
565+
$info['website']['notFoundPage'],
566+
),
567+
$output,
546568
);
547569

548570
$obj->delete();

0 commit comments

Comments
 (0)