-
Notifications
You must be signed in to change notification settings - Fork 1k
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
bshaffer
merged 10 commits into
GoogleCloudPlatform:main
from
thiyaguk09:object-soft-delete
Jun 10, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5a5b2c6
add sample list SD objects
thiyaguk09 9a5887d
add sample list SD version objects
thiyaguk09 1865155
add sample restore SD object
thiyaguk09 f0ac4f8
add sample get SD policy
thiyaguk09 7ec9afe
add sample set SD policy
thiyaguk09 47be490
add sample disable SD
thiyaguk09 c1c90bf
fix data type issue
thiyaguk09 57a67b5
Merge branch 'main' into object-soft-delete
thiyaguk09 c5295d1
Apply suggestions from code review
bshaffer ec2e14c
Merge branch 'main' into object-soft-delete
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]); | ||
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
and604800s
(with as
suffix as below)? Also, IIUC, the type of864000
is integer, the type of604800s
is string. Thanks :)There was a problem hiding this comment.
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, while604800s
(with thes
suffix) is a string. My bad on that — we’re using integers only here. I've updated it accordingly. Thanks for pointing it out!