Skip to content

Commit 3043452

Browse files
authored
adds Storage Labels samples (GoogleCloudPlatform#457)
1 parent b78b56a commit 3043452

File tree

8 files changed

+389
-1
lines changed

8 files changed

+389
-1
lines changed

storage/api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This simple command-line application demonstrates how to invoke Google Cloud Sto
2929
```sh
3030
bucket-acl Manage the ACL for Cloud Storage buckets.
3131
bucket-default-acl Manage the default ACL for Cloud Storage buckets.
32+
bucket-labels Manage Cloud Storage bucket labels
3233
buckets Manage Cloud Storage buckets
3334
encryption Upload and download Cloud Storage objects with encryption
3435
object-acl Manage the ACL for Cloud Storage objects

storage/api/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"src/functions/add_bucket_acl.php",
1313
"src/functions/add_bucket_default_acl.php",
1414
"src/functions/add_bucket_iam_member.php",
15+
"src/functions/add_bucket_label.php",
1516
"src/functions/add_object_acl.php",
1617
"src/functions/copy_object.php",
1718
"src/functions/create_bucket.php",
@@ -28,6 +29,7 @@
2829
"src/functions/get_bucket_acl_for_entity.php",
2930
"src/functions/get_bucket_default_acl.php",
3031
"src/functions/get_bucket_default_acl_for_entity.php",
32+
"src/functions/get_bucket_labels.php",
3133
"src/functions/get_object_acl.php",
3234
"src/functions/get_object_acl_for_entity.php",
3335
"src/functions/list_buckets.php",
@@ -36,6 +38,7 @@
3638
"src/functions/make_public.php",
3739
"src/functions/move_object.php",
3840
"src/functions/remove_bucket_iam_member.php",
41+
"src/functions/remove_bucket_label.php",
3942
"src/functions/rotate_encryption_key.php",
4043
"src/functions/upload_encrypted_object.php",
4144
"src/functions/upload_object.php",
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
/**
3+
* Copyright 2017 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+
namespace Google\Cloud\Samples\Storage;
19+
20+
use Symfony\Component\Console\Command\Command;
21+
use Symfony\Component\Console\Input\InputArgument;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
/**
27+
* Command line utility to manage Cloud Storage bucket labels.
28+
*
29+
* Usage: php storage.php bucket-labels
30+
*/
31+
class BucketLabelsCommand extends Command
32+
{
33+
protected function configure()
34+
{
35+
$this
36+
->setName('bucket-labels')
37+
->setDescription('Manage Cloud Storage bucket labels')
38+
->setHelp(<<
39+
The %command.name% command manages Cloud Storage Bucket labels.
40+
41+
php %command.full_name% --help
42+
43+
EOF
44+
)
45+
->addArgument(
46+
'bucket',
47+
InputArgument::REQUIRED,
48+
'The Cloud Storage bucket name'
49+
)
50+
->addArgument(
51+
'label',
52+
InputArgument::OPTIONAL,
53+
'The Cloud Storage label'
54+
)
55+
->addOption(
56+
'value',
57+
null,
58+
InputOption::VALUE_REQUIRED,
59+
'Set the value of the label'
60+
)
61+
->addOption(
62+
'remove',
63+
null,
64+
InputOption::VALUE_NONE,
65+
'Remove the buckets label'
66+
)
67+
;
68+
}
69+
70+
protected function execute(InputInterface $input, OutputInterface $output)
71+
{
72+
$bucketName = $input->getArgument('bucket');
73+
if ($label = $input->getArgument('label')) {
74+
if ($value = $input->getOption('value')) {
75+
add_bucket_label($bucketName, $label, $value);
76+
} elseif ($input->getOption('remove')) {
77+
remove_bucket_label($bucketName, $label);
78+
} else {
79+
throw new \Exception('You must provide --value or --remove '
80+
. 'when including a label name.');
81+
}
82+
} else {
83+
get_bucket_labels($bucketName);
84+
}
85+
}
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* Copyright 2017 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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START add_bucket_label]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Adds or updates a bucket label.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* @param string $labelName the name of the label to add.
34+
* @param string $labelValue the value of the label to add.
35+
*/
36+
function add_bucket_label($bucketName, $labelName, $labelValue)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$newLabels = [$labelName => $labelValue];
41+
$bucket->update(['labels' => $newLabels]);
42+
printf('Added label %s (%s) to %s' . PHP_EOL, $labelName, $labelValue, $bucketName);
43+
}
44+
# [END add_bucket_label]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* Copyright 2017 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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START get_bucket_labels]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Prints a list of a bucket's lables.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function get_bucket_labels($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$info = $bucket->info();
39+
if (isset($info['labels'])) {
40+
foreach ($info['labels'] as $labelKey => $labelValue) {
41+
printf('%s: %s' . PHP_EOL, $labelKey, $labelValue);
42+
}
43+
}
44+
}
45+
# [END get_bucket_labels]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* Copyright 2017 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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START remove_bucket_label]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Removes a label from a bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* @param string $labelName the name of the label to remove.
34+
*/
35+
function remove_bucket_label($bucketName, $labelName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$labels = [$labelName => null];
40+
$bucket->update(['labels' => $labels]);
41+
printf('Removed label %s from %s' . PHP_EOL, $labelName, $bucketName);
42+
}
43+
# [END remove_bucket_label]

storage/api/storage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@
1616
*/
1717
require __DIR__ . '/vendor/autoload.php';
1818

19-
use Google\Cloud\Samples\Storage\ObjectAclCommand;
2019
use Google\Cloud\Samples\Storage\BucketAclCommand;
2120
use Google\Cloud\Samples\Storage\BucketDefaultAclCommand;
21+
use Google\Cloud\Samples\Storage\BucketLabelsCommand;
2222
use Google\Cloud\Samples\Storage\BucketsCommand;
2323
use Google\Cloud\Samples\Storage\EncryptionCommand;
2424
use Google\Cloud\Samples\Storage\IamCommand;
25+
use Google\Cloud\Samples\Storage\ObjectAclCommand;
2526
use Google\Cloud\Samples\Storage\ObjectsCommand;
2627
use Google\Cloud\Samples\Storage\RequesterPaysCommand;
2728
use Symfony\Component\Console\Application;
2829

2930
$application = new Application();
3031
$application->add(new BucketAclCommand());
3132
$application->add(new BucketDefaultAclCommand());
33+
$application->add(new BucketLabelsCommand());
3234
$application->add(new BucketsCommand());
3335
$application->add(new EncryptionCommand());
3436
$application->add(new IamCommand());

0 commit comments

Comments
 (0)