Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions storagecontrol/src/managed_folder_create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_managed_folder_create]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\CreateManagedFolderRequest;
use Google\Cloud\Storage\Control\V2\ManagedFolder;

/**
* Create a new folder in an existing bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $managedFolderId The name of your folder inside the bucket.
* (e.g. 'my-folder')
*/
function managed_folder_create(string $bucketName, string $managedFolderId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->bucketName('_', $bucketName);

// $request = new CreateManagedFolderRequest([
// 'parent' => $formattedName,
// 'managedFolder' => new ManagedFolder(),
// 'managedFolderId' => $managedFolderId,
// ]);
Comment on lines +46 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this comment was not intentional, but I'm ok with it either way

$request = CreateManagedFolderRequest::build($formattedName, new ManagedFolder(), $managedFolderId);

$managedFolder = $storageControlClient->createManagedFolder($request);

printf('Performed createManagedFolder request for %s', $managedFolder->getName());
}
# [END storage_control_managed_folder_create]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
55 changes: 55 additions & 0 deletions storagecontrol/src/managed_folder_delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_managed_folder_delete]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\DeleteManagedFolderRequest;

/**
* Delete a folder in an existing bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $managedFolderId The id of your folder inside the bucket.
* (e.g. 'my-folder')
*/
function managed_folder_delete(string $bucketName, string $managedFolderId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->managedFolderName('_', $bucketName, $managedFolderId);

$request = DeleteManagedFolderRequest::build($formattedName);

$storageControlClient->deleteManagedFolder($request);

printf('Deleted Managed Folder %s', $managedFolderId);
}
# [END storage_control_managed_folder_delete]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
57 changes: 57 additions & 0 deletions storagecontrol/src/managed_folder_get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_managed_folder_get]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\GetManagedFolderRequest;

/**
* Get a folder in an existing bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $managedFolderId The name of your folder inside the bucket.
* (e.g. 'my-folder')
*/
function managed_folder_get(string $bucketName, string $managedFolderId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->managedFolderName('_', $bucketName, $managedFolderId);

$request = new GetManagedFolderRequest([
'name' => $formattedName,
]);

$managedFolder = $storageControlClient->getManagedFolder($request);

printf('Got Managed Folder %s', $managedFolder->getName());
}
# [END storage_control_managed_folder_get]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
57 changes: 57 additions & 0 deletions storagecontrol/src/managed_folders_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_managed_folder_list]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\ListManagedFoldersRequest;

/**
* List folders in an existing bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function managed_folders_list(string $bucketName): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->bucketName('_', $bucketName);

$request = new ListManagedFoldersRequest([
'parent' => $formattedName,
]);

$folders = $storageControlClient->listManagedFolders($request);

foreach ($folders as $folder) {
printf('%s bucket has managed folder %s' . PHP_EOL, $bucketName, $folder->getName());
}
}
# [END storage_control_managed_folder_list]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
65 changes: 65 additions & 0 deletions storagecontrol/test/StorageControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class StorageControlTest extends TestCase

private static $sourceBucket;
private static $folderId;
private static $managedFolderId;
private static $folderName;
private static $managedFolderName;
private static $storage;
private static $storageControlClient;
private static $location;
Expand All @@ -44,6 +46,7 @@ public static function setUpBeforeClass(): void
self::$location = 'us-west1';
$uniqueBucketId = time() . rand();
self::$folderId = time() . rand();
self::$managedFolderId = time() . rand();
self::$sourceBucket = self::$storage->createBucket(
sprintf('php-gcscontrol-sample-%s', $uniqueBucketId),
[
Expand All @@ -57,6 +60,11 @@ public static function setUpBeforeClass(): void
self::$sourceBucket->name(),
self::$folderId
);
self::$managedFolderName = self::$storageControlClient->managedFolderName(
'_',
self::$sourceBucket->name(),
self::$managedFolderId
);
}

public static function tearDownAfterClass(): void
Expand All @@ -79,6 +87,63 @@ public function testCreateFolder()
);
}

public function testManagedCreateFolder()
{
$output = $this->runFunctionSnippet('managed_folder_create', [
self::$sourceBucket->name(), self::$managedFolderId
]);

$this->assertStringContainsString(
sprintf('Performed createManagedFolder request for %s', self::$managedFolderName),
$output
);
}

/**
* @depends testCreateFolder
*/
public function testManagedGetFolder()
{
$output = $this->runFunctionSnippet('managed_folder_get', [
self::$sourceBucket->name(), self::$managedFolderId
]);

$this->assertStringContainsString(
sprintf('Got Managed Folder %s', self::$managedFolderName),
$output
);
}

/**
* @depends testManagedGetFolder
*/
public function testManagedListFolders()
{
$output = $this->runFunctionSnippet('managed_folders_list', [
self::$sourceBucket->name()
]);

$this->assertStringContainsString(
sprintf('%s bucket has managed folder %s', self::$sourceBucket->name(), self::$managedFolderName),
$output
);
}

/**
* @depends testManagedListFolders
*/
public function testManagedDeleteFolder()
{
$output = $this->runFunctionSnippet('managed_folder_delete', [
self::$sourceBucket->name(), self::$managedFolderId
]);

$this->assertStringContainsString(
sprintf('Deleted Managed Folder %s', self::$managedFolderId),
$output
);
}

/**
* @depends testCreateFolder
*/
Expand Down