Skip to content

Commit 3f4225e

Browse files
feat(storagecontrol): add HNS folders samples (#2013)
1 parent 4f76dfa commit 3f4225e

File tree

6 files changed

+433
-0
lines changed

6 files changed

+433
-0
lines changed

storagecontrol/src/create_folder.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_create_folder]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\CreateFolderRequest;
29+
30+
/**
31+
* Create a new 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 $folderName The name of your folder inside the bucket.
36+
* (e.g. 'my-folder')
37+
*/
38+
function create_folder(string $bucketName, string $folderName): void
39+
{
40+
$storageControlClient = new StorageControlClient();
41+
42+
// Set project to "_" to signify global bucket
43+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
44+
45+
$request = new CreateFolderRequest([
46+
'parent' => $formattedName,
47+
'folder_id' => $folderName,
48+
]);
49+
50+
$folder = $storageControlClient->createFolder($request);
51+
52+
printf('Created folder: %s', $folder->getName());
53+
}
54+
# [END storage_control_create_folder]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storagecontrol/src/delete_folder.php

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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_delete_folder]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\DeleteFolderRequest;
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 $folderName The name of your folder inside the bucket.
36+
* (e.g. 'my-folder')
37+
*/
38+
function delete_folder(string $bucketName, string $folderName): void
39+
{
40+
$storageControlClient = new StorageControlClient();
41+
42+
// Set project to "_" to signify global bucket
43+
$formattedName = $storageControlClient->folderName('_', $bucketName, $folderName);
44+
45+
$request = new DeleteFolderRequest([
46+
'name' => $formattedName,
47+
]);
48+
49+
$storageControlClient->deleteFolder($request);
50+
51+
printf('Deleted folder: %s', $folderName);
52+
}
53+
# [END storage_control_delete_folder]
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/src/get_folder.php

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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_get_folder]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\GetFolderRequest;
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 $folderName The name of your folder inside the bucket.
36+
* (e.g. 'my-folder')
37+
*/
38+
function get_folder(string $bucketName, string $folderName): void
39+
{
40+
$storageControlClient = new StorageControlClient();
41+
42+
// Set project to "_" to signify global bucket
43+
$formattedName = $storageControlClient->folderName('_', $bucketName, $folderName);
44+
45+
$request = new GetFolderRequest([
46+
'name' => $formattedName,
47+
]);
48+
49+
$folder = $storageControlClient->getFolder($request);
50+
51+
printf($folder->getName());
52+
}
53+
# [END storage_control_get_folder]
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/src/list_folders.php

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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_list_folders]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\ListFoldersRequest;
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 list_folders(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 ListFoldersRequest([
44+
'parent' => $formattedName,
45+
]);
46+
47+
$folders = $storageControlClient->listFolders($request);
48+
49+
foreach ($folders as $folder) {
50+
printf('Folder name: %s' . PHP_EOL, $folder->getName());
51+
}
52+
}
53+
# [END storage_control_list_folders]
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/src/rename_folder.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\StorageControl;
25+
26+
# [START storage_control_rename_folder]
27+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
28+
use Google\Cloud\Storage\Control\V2\RenameFolderRequest;
29+
30+
/**
31+
* Rename 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 $sourceFolder The source folder ID.
36+
* (e.g. 'my-folder')
37+
* @param string $destinationFolder The destination folder ID.
38+
* (e.g. 'my-folder')
39+
*/
40+
function rename_folder(string $bucketName, string $sourceFolder, string $destinationFolder): void
41+
{
42+
$storageControlClient = new StorageControlClient();
43+
44+
// Set project to "_" to signify global bucket
45+
$formattedName = $storageControlClient->folderName('_', $bucketName, $sourceFolder);
46+
47+
$request = new RenameFolderRequest([
48+
'name' => $formattedName,
49+
'destination_folder_id' => $destinationFolder,
50+
]);
51+
52+
$storageControlClient->renameFolder($request);
53+
54+
printf('Renamed folder %s to %s', $sourceFolder, $destinationFolder);
55+
}
56+
# [END storage_control_rename_folder]
57+
58+
// The following 2 lines are only needed to run the samples
59+
require_once __DIR__ . '/../../testing/sample_helpers.php';
60+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)