Skip to content

Commit ae61145

Browse files
authored
feat(Storage): add samples for soft-delete and restore buckets (#2067)
1 parent dbb74ac commit ae61145

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Copyright 2025 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+
/**
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\Storage;
25+
26+
# [START storage_get_soft_deleted_bucket]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get softDeleted bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*
35+
* @param string $generation The generation of the bucket to restore.
36+
* (e.g. '123456789')
37+
*/
38+
function get_soft_deleted_bucket(string $bucketName, string $generation): void
39+
{
40+
$options = ['generation' => $generation, 'softDeleted' => true];
41+
$storage = new StorageClient();
42+
$bucket = $storage->bucket($bucketName);
43+
$info = $bucket->info($options);
44+
45+
printf('Bucket: %s' . PHP_EOL, $bucketName);
46+
printf('Generation: %s' . PHP_EOL, $info['generation']);
47+
printf('SoftDeleteTime: %s' . PHP_EOL, $info['softDeleteTime']);
48+
printf('HardDeleteTime: %s' . PHP_EOL, $info['hardDeleteTime']);
49+
}
50+
# [END storage_get_soft_deleted_bucket]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* Copyright 2025 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+
/**
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\Storage;
25+
26+
# [START storage_list_soft_deleted_buckets]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* List all the soft deleted Cloud Storage buckets for the current project.
31+
*/
32+
function list_soft_deleted_buckets(): void
33+
{
34+
$storage = new StorageClient();
35+
$options = [ 'softDeleted' => true ];
36+
foreach ($storage->buckets($options) as $bucket) {
37+
printf('Bucket: %s' . PHP_EOL, $bucket->name());
38+
}
39+
}
40+
# [END storage_list_soft_deleted_buckets]
41+
42+
// The following 2 lines are only needed to run the samples
43+
require_once __DIR__ . '/../../testing/sample_helpers.php';
44+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Copyright 2025 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+
/**
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\Storage;
25+
26+
# [START storage_restore_soft_deleted_bucket]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Restore softDeleted bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*
35+
* @param string $generation The generation of the bucket to restore.
36+
* (e.g. '123456789')
37+
*/
38+
function restore_soft_deleted_bucket(string $bucketName, string $generation): void
39+
{
40+
$storage = new StorageClient();
41+
$storage->restore($bucketName, $generation);
42+
43+
printf('Soft deleted bucket %s was restored.' . PHP_EOL, $bucketName);
44+
}
45+
# [END storage_restore_soft_deleted_bucket]
46+
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/storageTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public function testListBuckets()
147147
$this->assertStringContainsString('Bucket:', $output);
148148
}
149149

150+
public function testListSoftDeletedBuckets()
151+
{
152+
$output = $this->runFunctionSnippet('list_soft_deleted_buckets');
153+
$this->assertStringContainsString('Bucket:', $output);
154+
}
155+
150156
public function testCreateGetDeleteBuckets()
151157
{
152158
$bucketName = sprintf('test-bucket-%s-%s', time(), rand());
@@ -559,6 +565,7 @@ public function testObjectGetKmsKey(string $objectName)
559565
$output,
560566
);
561567
}
568+
562569
public function testBucketVersioning()
563570
{
564571
$output = self::runFunctionSnippet('enable_versioning', [
@@ -860,6 +867,7 @@ public function testCreateBucketHnsEnabled()
860867
$output
861868
);
862869
$this->assertTrue($info['hierarchicalNamespace']['enabled']);
870+
$this->runFunctionSnippet('delete_bucket', [$bucketName]);
863871
}
864872

865873
public function testObjectCsekToCmek()
@@ -938,6 +946,50 @@ public function testGetBucketWithAutoclass()
938946
);
939947
}
940948

949+
public function testGetRestoreSoftDeletedBucket()
950+
{
951+
$bucketName = sprintf('test-soft-deleted-bucket-%s-%s', time(), rand());
952+
$bucket = self::$storage->createBucket($bucketName);
953+
954+
$this->assertTrue($bucket->exists());
955+
$generation = $bucket->info()['generation'];
956+
$bucket->delete();
957+
958+
$this->assertFalse($bucket->exists());
959+
960+
$options = ['generation' => $generation, 'softDeleted' => true];
961+
$softDeletedBucket = self::$storage->bucket($bucketName);
962+
$info = $softDeletedBucket->info($options);
963+
964+
$output = self::runFunctionSnippet('get_soft_deleted_bucket', [
965+
$bucketName,
966+
$generation
967+
]);
968+
$outputString = <<
969+
Bucket: {$bucketName}
970+
Generation: {$info['generation']}
971+
SoftDeleteTime: {$info['softDeleteTime']}
972+
HardDeleteTime: {$info['hardDeleteTime']}
973+
974+
EOF;
975+
$this->assertEquals($outputString, $output);
976+
977+
$output = self::runFunctionSnippet('restore_soft_deleted_bucket', [
978+
$bucketName,
979+
$generation
980+
]);
981+
982+
$this->assertTrue($bucket->exists());
983+
$this->assertEquals(
984+
sprintf(
985+
'Soft deleted bucket %s was restored.' . PHP_EOL,
986+
$bucketName
987+
),
988+
$output
989+
);
990+
$this->runFunctionSnippet('delete_bucket', [$bucketName]);
991+
}
992+
941993
public function testSetBucketWithAutoclass()
942994
{
943995
$bucket = self::$storage->createBucket(uniqid('samples-set-autoclass-'), [

0 commit comments

Comments
 (0)