Skip to content

Commit 1625b80

Browse files
feat(Storagecontrol): Adding new library sample (#1989)
1 parent 851664c commit 1625b80

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

storagecontrol/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Google Cloud Storage Control Samples
2+
3+
## Description
4+
5+
All code in the snippets directory demonstrate how to invoke
6+
[Cloud Storage Control][cloud-storagecontrol] from PHP.
7+
8+
[cloud-storage-control]: https://cloud.google.com/storage/docs/access-control
9+
10+
## Setup:
11+
12+
1. **Enable APIs** - [Enable the Storage Control Service API](https://console.cloud.google.com/flows/enableapi?apiid=storage.googleapis.com)
13+
and create a new project or select an existing project.
14+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
15+
and select "Service Account Key". Create a new service account, use the JSON key type, and
16+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
17+
to the path of the JSON key that was downloaded.
18+
3. **Clone the repo** and cd into this directory
19+
20+
```sh
21+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
22+
$ cd php-docs-samples/storagecontrol
23+
```
24+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
25+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
26+
(if composer is installed globally).
27+
28+
29+
## Samples
30+
31+
To run the Storage Control Quickstart Samples, run any of the files in `src/` on the CLI:
32+
33+
```
34+
$ php src/quickstart.php
35+
36+
Usage: quickstart.php $bucketName
37+
38+
@param string $bucketName The Storage bucket name
39+
```
40+
41+
Above command returns the storage layout configuration for a given bucket.
42+
43+
## The client library
44+
45+
This sample uses the [Cloud Storage Control Client Library for PHP][google-cloud-php-storage-control].
46+
You can read the documentation for more details on API usage and use GitHub
47+
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
48+
49+
[google-cloud-php-storage-control]: https://cloud.google.com/storage/docs/reference/rpc
50+
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
51+
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
52+
[google-cloud-sdk]: https://cloud.google.com/sdk/
53+
54+
## Contributing changes
55+
56+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
57+
58+
## Licensing
59+
60+
* See [LICENSE](../../LICENSE)

storagecontrol/composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require": {
3+
"google/cloud-storage-control": "0.1.0"
4+
},
5+
"require-dev": {
6+
"google/cloud-storage": "^1.41.3"
7+
}
8+
}

storagecontrol/phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../testing/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
<exclude>
8+
<directory>./vendor</directory>
9+
</exclude>
10+
<report>
11+
<clover outputFile="build/logs/clover.xml"/>
12+
</report>
13+
</coverage>
14+
<testsuites>
15+
<testsuite name="PHP storagecontrol test">
16+
<directory>test</directory>
17+
</testsuite>
18+
</testsuites>
19+
<logging/>
20+
<php>
21+
<env name="PHPUNIT_TESTS" value="1"/>
22+
</php>
23+
</phpunit>

storagecontrol/src/quickstart.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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+
// [START storage_control_quickstart_sample]
19+
// Includes the autoloader for libraries installed with composer
20+
require __DIR__ . '/../vendor/autoload.php';
21+
22+
// Imports the Google Cloud client library
23+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
24+
use Google\Cloud\Storage\Control\V2\GetStorageLayoutRequest;
25+
26+
// Instantiates a client
27+
$storageControlClient = new StorageControlClient();
28+
29+
// The name for the new bucket
30+
$bucketName = 'my-new-bucket';
31+
32+
// Set project to "_" to signify global bucket
33+
$formattedName = $storageControlClient->storageLayoutName('_', $bucketName);
34+
$request = (new GetStorageLayoutRequest())->setName($formattedName);
35+
36+
$response = $storageControlClient->getStorageLayout($request);
37+
38+
echo 'Performed get_storage_layout request for ' . $response->getName() . PHP_EOL;
39+
// [END storage_control_quickstart_sample]
40+
return $response;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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+
use Google\Cloud\Storage\Control\V2\StorageLayout;
19+
use Google\Cloud\Storage\StorageClient;
20+
use Google\Cloud\TestUtils\TestTrait;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class quickstartTest extends TestCase
24+
{
25+
use TestTrait;
26+
private $bucket;
27+
private $bucketName;
28+
private $storageClient;
29+
30+
public function setUp(): void
31+
{
32+
$this->bucketName = sprintf(
33+
'%s-%s',
34+
$this->requireEnv('GOOGLE_STORAGE_BUCKET'),
35+
time()
36+
);
37+
$this->storageClient = new StorageClient();
38+
$this->bucket = $this->storageClient->createBucket($this->bucketName);
39+
}
40+
41+
public function tearDown(): void
42+
{
43+
$this->bucket->delete();
44+
}
45+
46+
public function testQuickstart()
47+
{
48+
$file = $this->prepareFile();
49+
// Invoke quickstart.php
50+
ob_start();
51+
$response = include $file;
52+
$output = ob_get_clean();
53+
54+
// Make sure it looks correct
55+
$this->assertInstanceOf(StorageLayout::class, $response);
56+
$this->assertEquals(
57+
sprintf(
58+
'Performed get_storage_layout request for projects/_/buckets/%s/storageLayout' . PHP_EOL,
59+
$this->bucketName
60+
),
61+
$output
62+
);
63+
}
64+
65+
private function prepareFile()
66+
{
67+
$file = sys_get_temp_dir() . '/storage_control_quickstart.php';
68+
$contents = file_get_contents(__DIR__ . '/../src/quickstart.php');
69+
$contents = str_replace(
70+
['my-new-bucket', '__DIR__'],
71+
[$this->bucketName, sprintf('"%s"', __DIR__)],
72+
$contents
73+
);
74+
file_put_contents($file, $contents);
75+
return $file;
76+
}
77+
}

0 commit comments

Comments
 (0)