Skip to content

Commit 7f75086

Browse files
committed
Storage requester pays sample code + tests
1 parent 3d8e2fd commit 7f75086

10 files changed

+580
-106
lines changed

storage/api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This simple command-line application demonstrates how to invoke Google Cloud Sto
3333
encryption Upload and download Cloud Storage objects with encryption
3434
object-acl Manage the ACL for Cloud Storage objects
3535
objects Manage Cloud Storage objects
36+
requester-pays Manage Cloud Storage requester pays buckets and objects
3637
```
3738
6. Run `php storage.php COMMAND --help` to print information about the usage of each command.
3839

storage/api/composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
"src/functions/rotate_encryption_key.php",
4040
"src/functions/upload_encrypted_object.php",
4141
"src/functions/upload_object.php",
42-
"src/functions/view_bucket_iam_members.php"
42+
"src/functions/view_bucket_iam_members.php",
43+
"src/functions/enable_requester_pays.php",
44+
"src/functions/disable_requester_pays.php",
45+
"src/functions/get_requester_pays_status.php",
46+
"src/functions/download_file_requester_pays.php"
4347
]
4448
},
4549
"require-dev": {

storage/api/composer.lock

Lines changed: 116 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 requester pays buckets.
28+
*
29+
* Usage: php storage.php requester-pays
30+
*/
31+
class RequesterPaysCommand extends Command
32+
{
33+
protected function configure()
34+
{
35+
$this
36+
->setName('requester-pays')
37+
->setDescription('Manage Cloud Storage requester pays buckets.')
38+
->setHelp(<<
39+
The %command.name% command manages Cloud Storage requester pays buckets.
40+
41+
php %command.full_name% --help
42+
43+
EOF
44+
)
45+
->addArgument(
46+
'project',
47+
InputArgument::REQUIRED,
48+
'Your billable Google Cloud Project ID'
49+
)
50+
->addArgument(
51+
'bucket',
52+
InputArgument::REQUIRED,
53+
'The Cloud Storage requester pays bucket name'
54+
)
55+
->addArgument(
56+
'object',
57+
InputArgument::OPTIONAL,
58+
'The Cloud Storage requester pays object name'
59+
)
60+
->addArgument(
61+
'download-to',
62+
null,
63+
InputArgument::OPTIONAL,
64+
'Path to store the dowloaded file'
65+
)
66+
->addOption(
67+
'enable',
68+
null,
69+
InputOption::VALUE_NONE,
70+
'Enable requester pays on a Cloud Storage bucket'
71+
)
72+
->addOption(
73+
'disable',
74+
null,
75+
InputOption::VALUE_NONE,
76+
'Disable requester pays on a Cloud Storage bucket'
77+
)
78+
->addOption(
79+
'check-status',
80+
null,
81+
InputOption::VALUE_NONE,
82+
'Check requester pays status on a Cloud Storage bucekt'
83+
)
84+
;
85+
}
86+
87+
protected function execute(InputInterface $input, OutputInterface $output)
88+
{
89+
$projectId = $input->getArgument('project');
90+
$bucketName = $input->getArgument('bucket');
91+
if ($objectName = $input->getArgument('object')) {
92+
if ($destination = $input->getArgument('download-to')) {
93+
download_file_requester_pays($projectId, $bucketName, $objectName, $destination);
94+
}
95+
} else if ($input->getOption('enable')) {
96+
enable_requester_pays($projectId, $bucketName);
97+
} else if ($input->getOption('disable')) {
98+
disable_requester_pays($projectId, $bucketName);
99+
} else if ($input->getOption('check-status')) {
100+
get_requester_pays_status($projectId, $bucketName);
101+
}
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 disable_requester_pays]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Disable a bucket's requesterpays metadata.
31+
*
32+
* @param string $projectId Your Google Cloud project ID.
33+
* @param string $bucketName Name of your Google Cloud Storage bucket.
34+
*
35+
* @return void
36+
*/
37+
function disable_requester_pays($projectId, $bucketName)
38+
{
39+
$storage = new StorageClient([
40+
'projectId' => $projectId
41+
]);
42+
$bucket = $storage->bucket($bucketName);
43+
$bucket->update([
44+
'billing' => [
45+
'requesterPays' => False
46+
]
47+
]);
48+
printf('Requester pays has been disabled for %s' . PHP_EOL, $bucketName);
49+
}
50+
# [END disable_requester_pays]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 download_file_requester_pays]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Download file using specified project as requester
31+
*
32+
* @param string $projectId Your Google Cloud billable project ID.
33+
* @param string $bucketName A Google Cloud Storage bucket name.
34+
* @param string $objectName Name of object in Google Cloud Storage to download locally.
35+
* @param string $destination Path to local file to save.
36+
*
37+
* @return void
38+
*/
39+
function download_file_requester_pays($projectId, $bucketName, $objectName, $destination)
40+
{
41+
$storage = new StorageClient([
42+
'projectId' => $projectId
43+
]);
44+
$bucket = $storage->bucket($bucketName, true);
45+
$object = $bucket->object($objectName);
46+
$object->downloadToFile($destination);
47+
printf('Downloaded gs://%s/%s to %s using requester-pays requests.' . PHP_EOL,
48+
$bucketName, $objectName, basename($destination));
49+
}
50+
# [END download_file_requester_pays]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 enable_requester_pays]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Enable a bucket's requesterpays metadata.
31+
*
32+
* @param string $projectId Your Google Cloud project ID.
33+
* @param string $bucketName Name of your Google Cloud Storage bucket.
34+
*
35+
* @return void
36+
*/
37+
function enable_requester_pays($projectId, $bucketName)
38+
{
39+
$storage = new StorageClient([
40+
'projectId' => $projectId
41+
]);
42+
$bucket = $storage->bucket($bucketName);
43+
$bucket->update([
44+
'billing' => [
45+
'requesterPays' => True
46+
]
47+
]);
48+
printf('Requester pays has been enabled for %s' . PHP_EOL, $bucketName);
49+
}
50+
# [END enable_requester_pays]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_requester_pays_status]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get a bucket's requesterpays metadata.
31+
*
32+
* @param string $projectId Your Google Cloud project ID.
33+
* @param string $bucketName Name of your Google Cloud Storage bucket.
34+
*
35+
* @return void
36+
*/
37+
function get_requester_pays_status($projectId, $bucketName)
38+
{
39+
$storage = new StorageClient([
40+
'projectId' => $projectId
41+
]);
42+
$bucket = $storage->bucket($bucketName);
43+
$bucketInformation = $bucket->info();
44+
$requesterPaysStatus = $bucketInformation['billing']['requesterPays'];
45+
if ($requesterPaysStatus) {
46+
printf('Requester Pays is enabled for %s' . PHP_EOL, $bucketName);
47+
} else {
48+
printf('Requester Pays is disabled for %s' . PHP_EOL, $bucketName);
49+
}
50+
}
51+
# [END get_requester_pays_status]

storage/api/storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Google\Cloud\Samples\Storage\EncryptionCommand;
2424
use Google\Cloud\Samples\Storage\IamCommand;
2525
use Google\Cloud\Samples\Storage\ObjectsCommand;
26+
use Google\Cloud\Samples\Storage\RequesterPaysCommand;
2627
use Symfony\Component\Console\Application;
2728

2829
$application = new Application();
@@ -33,4 +34,5 @@
3334
$application->add(new IamCommand());
3435
$application->add(new ObjectAclCommand());
3536
$application->add(new ObjectsCommand());
37+
$application->add(new RequesterPaysCommand());
3638
$application->run();

0 commit comments

Comments
 (0)