Skip to content

Commit 1e27c8c

Browse files
feat(storagecontrol): add samples for managed folders (#2016)
1 parent 3f4225e commit 1e27c8c

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/storagecontrol/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_managed_folder_create]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\CreateManagedFolderRequest;
29+
use Google\Cloud\Storage\Control\V2\ManagedFolder;
30+
31+
/**
32+
* Create a new folder in an existing bucket.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $managedFolderId The name of your folder inside the bucket.
37+
* (e.g. 'my-folder')
38+
*/
39+
function managed_folder_create(string $bucketName, string $managedFolderId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
45+
46+
// $request = new CreateManagedFolderRequest([
47+
// 'parent' => $formattedName,
48+
// 'managedFolder' => new ManagedFolder(),
49+
// 'managedFolderId' => $managedFolderId,
50+
// ]);
51+
$request = CreateManagedFolderRequest::build($formattedName, new ManagedFolder(), $managedFolderId);
52+
53+
$managedFolder = $storageControlClient->createManagedFolder($request);
54+
55+
printf('Performed createManagedFolder request for %s', $managedFolder->getName());
56+
}
57+
# [END storage_control_managed_folder_create]
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/storagecontrol/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_managed_folder_delete]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\DeleteManagedFolderRequest;
29+
30+
/**
31+
* Delete a folder in an existing bucket.
32+
*
33+
* @param string $bucketName The name of your Cloud Storage bucket.
34+
* (e.g. 'my-bucket')
35+
* @param string $managedFolderId The id of your folder inside the bucket.
36+
* (e.g. 'my-folder')
37+
*/
38+
function managed_folder_delete(string $bucketName, string $managedFolderId): void
39+
{
40+
$storageControlClient = new StorageControlClient();
41+
42+
// Set project to "_" to signify global bucket
43+
$formattedName = $storageControlClient->managedFolderName('_', $bucketName, $managedFolderId);
44+
45+
$request = DeleteManagedFolderRequest::build($formattedName);
46+
47+
$storageControlClient->deleteManagedFolder($request);
48+
49+
printf('Deleted Managed Folder %s', $managedFolderId);
50+
}
51+
# [END storage_control_managed_folder_delete]
52+
53+
// The following 2 lines are only needed to run the samples
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/storagecontrol/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_managed_folder_get]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\GetManagedFolderRequest;
29+
30+
/**
31+
* Get a folder in an existing bucket.
32+
*
33+
* @param string $bucketName The name of your Cloud Storage bucket.
34+
* (e.g. 'my-bucket')
35+
* @param string $managedFolderId The name of your folder inside the bucket.
36+
* (e.g. 'my-folder')
37+
*/
38+
function managed_folder_get(string $bucketName, string $managedFolderId): void
39+
{
40+
$storageControlClient = new StorageControlClient();
41+
42+
// Set project to "_" to signify global bucket
43+
$formattedName = $storageControlClient->managedFolderName('_', $bucketName, $managedFolderId);
44+
45+
$request = new GetManagedFolderRequest([
46+
'name' => $formattedName,
47+
]);
48+
49+
$managedFolder = $storageControlClient->getManagedFolder($request);
50+
51+
printf('Got Managed Folder %s', $managedFolder->getName());
52+
}
53+
# [END storage_control_managed_folder_get]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/storagecontrol/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_managed_folder_list]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\ListManagedFoldersRequest;
29+
30+
/**
31+
* List folders in an existing bucket.
32+
*
33+
* @param string $bucketName The name of your Cloud Storage bucket.
34+
* (e.g. 'my-bucket')
35+
*/
36+
function managed_folders_list(string $bucketName): void
37+
{
38+
$storageControlClient = new StorageControlClient();
39+
40+
// Set project to "_" to signify global bucket
41+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
42+
43+
$request = new ListManagedFoldersRequest([
44+
'parent' => $formattedName,
45+
]);
46+
47+
$folders = $storageControlClient->listManagedFolders($request);
48+
49+
foreach ($folders as $folder) {
50+
printf('%s bucket has managed folder %s' . PHP_EOL, $bucketName, $folder->getName());
51+
}
52+
}
53+
# [END storage_control_managed_folder_list]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storagecontrol/test/StorageControlTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class StorageControlTest extends TestCase
3131

3232
private static $sourceBucket;
3333
private static $folderId;
34+
private static $managedFolderId;
3435
private static $folderName;
36+
private static $managedFolderName;
3537
private static $storage;
3638
private static $storageControlClient;
3739
private static $location;
@@ -44,6 +46,7 @@ public static function setUpBeforeClass(): void
4446
self::$location = 'us-west1';
4547
$uniqueBucketId = time() . rand();
4648
self::$folderId = time() . rand();
49+
self::$managedFolderId = time() . rand();
4750
self::$sourceBucket = self::$storage->createBucket(
4851
sprintf('php-gcscontrol-sample-%s', $uniqueBucketId),
4952
[
@@ -57,6 +60,11 @@ public static function setUpBeforeClass(): void
5760
self::$sourceBucket->name(),
5861
self::$folderId
5962
);
63+
self::$managedFolderName = self::$storageControlClient->managedFolderName(
64+
'_',
65+
self::$sourceBucket->name(),
66+
self::$managedFolderId
67+
);
6068
}
6169

6270
public static function tearDownAfterClass(): void
@@ -79,6 +87,63 @@ public function testCreateFolder()
7987
);
8088
}
8189

90+
public function testManagedCreateFolder()
91+
{
92+
$output = $this->runFunctionSnippet('managed_folder_create', [
93+
self::$sourceBucket->name(), self::$managedFolderId
94+
]);
95+
96+
$this->assertStringContainsString(
97+
sprintf('Performed createManagedFolder request for %s', self::$managedFolderName),
98+
$output
99+
);
100+
}
101+
102+
/**
103+
* @depends testCreateFolder
104+
*/
105+
public function testManagedGetFolder()
106+
{
107+
$output = $this->runFunctionSnippet('managed_folder_get', [
108+
self::$sourceBucket->name(), self::$managedFolderId
109+
]);
110+
111+
$this->assertStringContainsString(
112+
sprintf('Got Managed Folder %s', self::$managedFolderName),
113+
$output
114+
);
115+
}
116+
117+
/**
118+
* @depends testManagedGetFolder
119+
*/
120+
public function testManagedListFolders()
121+
{
122+
$output = $this->runFunctionSnippet('managed_folders_list', [
123+
self::$sourceBucket->name()
124+
]);
125+
126+
$this->assertStringContainsString(
127+
sprintf('%s bucket has managed folder %s', self::$sourceBucket->name(), self::$managedFolderName),
128+
$output
129+
);
130+
}
131+
132+
/**
133+
* @depends testManagedListFolders
134+
*/
135+
public function testManagedDeleteFolder()
136+
{
137+
$output = $this->runFunctionSnippet('managed_folder_delete', [
138+
self::$sourceBucket->name(), self::$managedFolderId
139+
]);
140+
141+
$this->assertStringContainsString(
142+
sprintf('Deleted Managed Folder %s', self::$managedFolderId),
143+
$output
144+
);
145+
}
146+
82147
/**
83148
* @depends testCreateFolder
84149
*/

0 commit comments

Comments
 (0)