Skip to content

feat(storage): add samples for soft delete (objects) #2085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 10, 2025
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
55 changes: 55 additions & 0 deletions storage/src/disable_soft_delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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_disable_soft_delete]
use Google\Cloud\Storage\StorageClient;

/**
* Disable bucket's soft delete.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function disable_soft_delete(string $bucketName): void
{
try {
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$x = $bucket->update([
'softDeletePolicy' => [
'retentionDurationSeconds' => 0,
],
]);
printf('Bucket %s soft delete policy was disabled' . PHP_EOL, $bucketName);
} catch (\Throwable $th) {
print_r($th);
}

}
# [END storage_disable_soft_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);
61 changes: 61 additions & 0 deletions storage/src/get_soft_delete_policy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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_delete_policy]
use Google\Cloud\Storage\StorageClient;

/**
* Gets a bucket soft delete policy.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function get_soft_delete_policy(string $bucketName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$bucket->reload();

if ($bucket->info()['softDeletePolicy']['retentionDurationSeconds'] === '0') {
printf('Bucket %s soft delete policy was disabled' . PHP_EOL, $bucketName);
} else {
printf('Soft delete Policy for ' . $bucketName . PHP_EOL);
printf(
'Soft delete Period: %d seconds' . PHP_EOL,
$bucket->info()['softDeletePolicy']['retentionDurationSeconds']
);
if ($bucket->info()['softDeletePolicy']['effectiveTime']) {
printf(
'Effective Time: %s' . PHP_EOL,
$bucket->info()['softDeletePolicy']['effectiveTime']
);
}
}
}
# [END storage_get_soft_delete_policy]

// 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);
50 changes: 50 additions & 0 deletions storage/src/list_soft_deleted_object_versions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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_object_versions]
use Google\Cloud\Storage\StorageClient;

/**
* List Cloud Storage bucket soft deleted objects.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $objectName The name of your Cloud Storage object.
* (e.g. 'my-object')
*/
function list_soft_deleted_object_versions(string $bucketName, string $objectName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$options = ['softDeleted' => true, 'matchGlob' => $objectName];
foreach ($bucket->objects($options) as $object) {
printf('Object: %s' . PHP_EOL, $object->name());
}
}
# [END storage_list_soft_deleted_object_versions]

// 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);
48 changes: 48 additions & 0 deletions storage/src/list_soft_deleted_objects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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_objects]
use Google\Cloud\Storage\StorageClient;

/**
* List Cloud Storage bucket soft deleted object versions.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function list_soft_deleted_objects(string $bucketName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$options = ['softDeleted' => true];
foreach ($bucket->objects($options) as $object) {
printf('Object: %s' . PHP_EOL, $object->name());
}
}
# [END storage_list_soft_deleted_objects]

// 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);
51 changes: 51 additions & 0 deletions storage/src/restore_soft_deleted_object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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_object]
use Google\Cloud\Storage\StorageClient;

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

printf('Soft deleted object %s was restored.' . PHP_EOL, $objectName);
}
# [END storage_restore_object]

// 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);
50 changes: 50 additions & 0 deletions storage/src/set_soft_delete_policy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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_set_soft_delete_policy]
use Google\Cloud\Storage\StorageClient;

/**
* Sets a bucket's soft delete policy.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function set_soft_delete_policy(string $bucketName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$bucket->update([
'softDeletePolicy' => [
'retentionDurationSeconds' => 864000,
Copy link

@TimothyLiuAtGoogle TimothyLiuAtGoogle May 21, 2025

Choose a reason for hiding this comment

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

What's the difference between the value 864000 and 604800s (with a s suffix as below)? Also, IIUC, the type of 864000 is integer, the type of 604800s is string. Thanks :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right — the value 864000 is an integer, while 604800s (with the s suffix) is a string. My bad on that — we’re using integers only here. I've updated it accordingly. Thanks for pointing it out!

],
]);
printf('Bucket %s soft delete policy set to 10 days' . PHP_EOL, $bucketName);
}
# [END storage_set_soft_delete_policy]

// 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);
Loading