Skip to content

Commit 584f170

Browse files
frankynbshaffer
authored andcommitted
[Storage] Add bucket_metadata sample (GoogleCloudPlatform#937)
1 parent 5b7eb42 commit 584f170

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

storage/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"src/enable_default_kms_key.php",
3030
"src/enable_requester_pays.php",
3131
"src/generate_encryption_key.php",
32+
"src/bucket_metadata.php",
3233
"src/get_bucket_acl.php",
3334
"src/get_bucket_acl_for_entity.php",
3435
"src/get_bucket_default_acl.php",

storage/src/bucket_metadata.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* Copyright 2019 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/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_bucket_metadata]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get bucket metadata.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*
34+
* @return void
35+
*/
36+
function get_bucket_metadata($bucketName)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$info = $bucket->info();
41+
42+
printf("Bucket Metadata: %s" . PHP_EOL, print_r($info));
43+
}
44+
# [END storage_get_bucket_metadata]

storage/storage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
$application->add(new Command('buckets'))
126126
->setDescription('Manage Cloud Storage buckets')
127127
->setHelp(<<
128-
The %command.name% command manages Cloud Storage ACL.
128+
The %command.name% command manages buckets.
129129
130130
php %command.full_name% --help
131131
@@ -134,12 +134,15 @@
134134
->addArgument('bucket', InputArgument::OPTIONAL, 'The Cloud Storage bucket name')
135135
->addOption('create', null, InputOption::VALUE_NONE, 'Create the bucket')
136136
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the bucket')
137+
->addOption('metadata', null, InputOption::VALUE_NONE, 'Get the bucket metadata')
137138
->setCode(function ($input, $output) {
138139
if ($bucketName = $input->getArgument('bucket')) {
139140
if ($input->getOption('create')) {
140141
create_bucket($bucketName);
141142
} elseif ($input->getOption('delete')) {
142143
delete_bucket($bucketName);
144+
} elseif ($input->getOption('metadata')) {
145+
get_bucket_metadata($bucketName);
143146
} else {
144147
throw new \Exception('Supply --create or --delete with bucket name');
145148
}

storage/test/storageTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testListBuckets()
112112
$this->assertContains("Bucket:", $output);
113113
}
114114

115-
public function testCreateAndDeleteBuckets()
115+
public function testCreateGetDeleteBuckets()
116116
{
117117
$bucketName = sprintf('test-bucket-%s-%s', time(), rand());
118118
$bucket = self::$storage->bucket($bucketName);
@@ -127,6 +127,13 @@ public function testCreateAndDeleteBuckets()
127127
$bucket->reload();
128128
$this->assertTrue($bucket->exists());
129129

130+
$output = $this->runCommand('buckets', [
131+
'bucket' => $bucketName,
132+
'--metadata' => true,
133+
]);
134+
135+
$this->assertContains("Bucket Metadata:", $output);
136+
130137
$output = $this->runCommand('buckets', [
131138
'bucket' => $bucketName,
132139
'--delete' => true,

0 commit comments

Comments
 (0)