Skip to content

Commit 0248eae

Browse files
authored
feat(Storage): add samples for soft delete (objects) (#2085)
1 parent a0b6245 commit 0248eae

8 files changed

+478
-0
lines changed

storage/src/disable_soft_delete.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
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_disable_soft_delete]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Disable bucket's soft delete.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function disable_soft_delete(string $bucketName): void
36+
{
37+
try {
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$x = $bucket->update([
41+
'softDeletePolicy' => [
42+
'retentionDurationSeconds' => 0,
43+
],
44+
]);
45+
printf('Bucket %s soft delete policy was disabled' . PHP_EOL, $bucketName);
46+
} catch (\Throwable $th) {
47+
print_r($th);
48+
}
49+
50+
}
51+
# [END storage_disable_soft_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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
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_delete_policy]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Gets a bucket soft delete policy.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function get_soft_delete_policy(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$bucket->reload();
40+
41+
if ($bucket->info()['softDeletePolicy']['retentionDurationSeconds'] === '0') {
42+
printf('Bucket %s soft delete policy was disabled' . PHP_EOL, $bucketName);
43+
} else {
44+
printf('Soft delete Policy for ' . $bucketName . PHP_EOL);
45+
printf(
46+
'Soft delete Period: %d seconds' . PHP_EOL,
47+
$bucket->info()['softDeletePolicy']['retentionDurationSeconds']
48+
);
49+
if ($bucket->info()['softDeletePolicy']['effectiveTime']) {
50+
printf(
51+
'Effective Time: %s' . PHP_EOL,
52+
$bucket->info()['softDeletePolicy']['effectiveTime']
53+
);
54+
}
55+
}
56+
}
57+
# [END storage_get_soft_delete_policy]
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
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_object_versions]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* List Cloud Storage bucket soft deleted objects.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
* @param string $objectName The name of your Cloud Storage object.
35+
* (e.g. 'my-object')
36+
*/
37+
function list_soft_deleted_object_versions(string $bucketName, string $objectName): void
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$options = ['softDeleted' => true, 'matchGlob' => $objectName];
42+
foreach ($bucket->objects($options) as $object) {
43+
printf('Object: %s' . PHP_EOL, $object->name());
44+
}
45+
}
46+
# [END storage_list_soft_deleted_object_versions]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
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_objects]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* List Cloud Storage bucket soft deleted object versions.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function list_soft_deleted_objects(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$options = ['softDeleted' => true];
40+
foreach ($bucket->objects($options) as $object) {
41+
printf('Object: %s' . PHP_EOL, $object->name());
42+
}
43+
}
44+
# [END storage_list_soft_deleted_objects]
45+
46+
// The following 2 lines are only needed to run the samples
47+
require_once __DIR__ . '/../../testing/sample_helpers.php';
48+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
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_object]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Restore softDeleted objects.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
* @param string $objectName The name of your Cloud Storage object.
35+
* (e.g. 'my-object')
36+
* @param string $generation The generation of the bucket to restore.
37+
* (e.g. '123456789')
38+
*/
39+
function restore_soft_deleted_object(string $bucketName, string $objectName, string $generation): void
40+
{
41+
$storage = new StorageClient();
42+
$bucket = $storage->bucket($bucketName);
43+
$bucket->restore($objectName, $generation);
44+
45+
printf('Soft deleted object %s was restored.' . PHP_EOL, $objectName);
46+
}
47+
# [END storage_restore_object]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
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_set_soft_delete_policy]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Sets a bucket's soft delete policy.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function set_soft_delete_policy(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$bucket->update([
40+
'softDeletePolicy' => [
41+
'retentionDurationSeconds' => 864000,
42+
],
43+
]);
44+
printf('Bucket %s soft delete policy set to 10 days' . PHP_EOL, $bucketName);
45+
}
46+
# [END storage_set_soft_delete_policy]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)