Skip to content
Merged
54 changes: 54 additions & 0 deletions storage/src/get_soft_deleted_bucket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* 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/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_get_soft_deleted_bucket]
use Google\Cloud\Storage\StorageClient;

/**
* Get softDeleted bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*
* @param string $generation The generation of the bucket to restore.
* (e.g. '123456789')
*/
function get_soft_deleted_bucket(string $bucketName, string $generation): void
{
$options = ['generation' => $generation, 'softDeleted' => true];
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$info = $bucket->info($options);

printf('Bucket: %s' . PHP_EOL, $bucketName);
printf('Generation: %s' . PHP_EOL, $info['generation']);
printf('SoftDeleteTime: %s' . PHP_EOL, $info['softDeleteTime']);
printf('HardDeleteTime: %s' . PHP_EOL, $info['hardDeleteTime']);
}
# [END storage_get_soft_deleted_bucket]

// 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);
44 changes: 44 additions & 0 deletions storage/src/list_soft_deleted_buckets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* 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/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_list_soft_deleted_buckets]
use Google\Cloud\Storage\StorageClient;

/**
* List all the soft deleted Cloud Storage buckets for the current project.
*/
function list_soft_deleted_buckets(): void
{
$storage = new StorageClient();
$options = [ 'softDeleted' => true ];
foreach ($storage->buckets($options) as $bucket) {
printf('Bucket: %s' . PHP_EOL, $bucket->name());
}
}
# [END storage_list_soft_deleted_buckets]

// 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);
49 changes: 49 additions & 0 deletions storage/src/restore_soft_deleted_bucket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* 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/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_restore_soft_deleted_bucket]
use Google\Cloud\Storage\StorageClient;

/**
* Restore softDeleted bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*
* @param string $generation The generation of the bucket to restore.
* (e.g. '123456789')
*/
function restore_soft_deleted_bucket(string $bucketName, string $generation): void
{
$storage = new StorageClient();
$storage->restore($bucketName, $generation);

printf('Soft deleted bucket %s was restored.' . PHP_EOL, $bucketName);
}
# [END storage_restore_soft_deleted_bucket]

// 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);
52 changes: 52 additions & 0 deletions storage/test/storageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public function testListBuckets()
$this->assertStringContainsString('Bucket:', $output);
}

public function testListSoftDeletedBuckets()
{
$output = $this->runFunctionSnippet('list_soft_deleted_buckets');
$this->assertStringContainsString('Bucket:', $output);
}

public function testCreateGetDeleteBuckets()
{
$bucketName = sprintf('test-bucket-%s-%s', time(), rand());
Expand Down Expand Up @@ -559,6 +565,7 @@ public function testObjectGetKmsKey(string $objectName)
$output,
);
}

public function testBucketVersioning()
{
$output = self::runFunctionSnippet('enable_versioning', [
Expand Down Expand Up @@ -860,6 +867,7 @@ public function testCreateBucketHnsEnabled()
$output
);
$this->assertTrue($info['hierarchicalNamespace']['enabled']);
$this->runFunctionSnippet('delete_bucket', [$bucketName]);
}

public function testObjectCsekToCmek()
Expand Down Expand Up @@ -938,6 +946,50 @@ public function testGetBucketWithAutoclass()
);
}

public function testGetRestoreSoftDeletedBucket()
{
$bucketName = sprintf('test-soft-deleted-bucket-%s-%s', time(), rand());
$bucket = self::$storage->createBucket($bucketName);

$this->assertTrue($bucket->exists());
$generation = $bucket->info()['generation'];
$bucket->delete();

$this->assertFalse($bucket->exists());

$options = ['generation' => $generation, 'softDeleted' => true];
$softDeletedBucket = self::$storage->bucket($bucketName);
$info = $softDeletedBucket->info($options);

$output = self::runFunctionSnippet('get_soft_deleted_bucket', [
$bucketName,
$generation
]);
$outputString = <<<EOF
Bucket: {$bucketName}
Generation: {$info['generation']}
SoftDeleteTime: {$info['softDeleteTime']}
HardDeleteTime: {$info['hardDeleteTime']}

EOF;
$this->assertEquals($outputString, $output);

$output = self::runFunctionSnippet('restore_soft_deleted_bucket', [
$bucketName,
$generation
]);

$this->assertTrue($bucket->exists());
$this->assertEquals(
sprintf(
'Soft deleted bucket %s was restored.' . PHP_EOL,
$bucketName
),
$output
);
$this->runFunctionSnippet('delete_bucket', [$bucketName]);
}

public function testSetBucketWithAutoclass()
{
$bucket = self::$storage->createBucket(uniqid('samples-set-autoclass-'), [
Expand Down