-
Notifications
You must be signed in to change notification settings - Fork 172
samples(storage): add samples for soft delete objects #1486
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
shubham-up-47
merged 10 commits into
googleapis:main
from
shubham-up-47:soft_delete_samples
Jun 7, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ff4cb6d
add samples and test cases for soft delete objects
shubham-up-47 7ef37d8
file formatting
shubham-up-47 b54f8c5
fix lint
shubham-up-47 b6928d5
Merge branch 'googleapis:main' into soft_delete_samples
shubham-up-47 2de7c06
minor fixes
shubham-up-47 399171a
minor fix
shubham-up-47 bf282f2
Merge branch 'main' into soft_delete_samples
shubham-up-47 b7538fd
changing pytest fixture type
shubham-up-47 4c325a0
resolving comments
shubham-up-47 993e8ed
removing a comment
shubham-up-47 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
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,40 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2025 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_disable_soft_delete] | ||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def disable_soft_delete(bucket_name): | ||
| """Disable soft-delete policy for the bucket.""" | ||
| # bucket_name = "your-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
|
|
||
| # Setting the retention duration to 0 disables soft-delete. | ||
| bucket.soft_delete_policy.retention_duration_seconds = 0 | ||
| bucket.patch() | ||
|
|
||
| print(f"Soft-delete policy is disabled for bucket {bucket_name}") | ||
|
|
||
|
|
||
| # [END storage_disable_soft_delete] | ||
|
|
||
| if __name__ == "__main__": | ||
| disable_soft_delete(bucket_name=sys.argv[1]) |
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,47 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2025 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_get_soft_delete_policy] | ||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def get_soft_delete_policy(bucket_name): | ||
| """Gets the soft-delete policy of the bucket""" | ||
| # bucket_name = "your-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
|
|
||
| print(f"Soft-delete policy for {bucket_name}") | ||
| if ( | ||
| bucket.soft_delete_policy | ||
| and bucket.soft_delete_policy.retention_duration_seconds | ||
| ): | ||
| print("Object soft-delete policy is enabled") | ||
| print( | ||
| f"Object retention duration: {bucket.soft_delete_policy.retention_duration_seconds} seconds" | ||
| ) | ||
| print(f"Policy effective time: {bucket.soft_delete_policy.effective_time}") | ||
| else: | ||
| print("Object soft-delete policy is disabled") | ||
|
|
||
|
|
||
| # [END storage_get_soft_delete_policy] | ||
|
|
||
| if __name__ == "__main__": | ||
| get_soft_delete_policy(bucket_name=sys.argv[1]) |
41 changes: 41 additions & 0 deletions
41
samples/snippets/storage_list_soft_deleted_object_versions.py
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,41 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2025 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_list_soft_deleted_object_versions] | ||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def list_soft_deleted_object_versions(bucket_name, blob_name): | ||
| """Lists all versions of a soft-deleted object in the bucket.""" | ||
| # bucket_name = "your-bucket-name" | ||
| # blob_name = "your-object-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| blobs = storage_client.list_blobs(bucket_name, prefix=blob_name, soft_deleted=True) | ||
|
|
||
| # Note: The call returns a response only when the iterator is consumed. | ||
| for blob in blobs: | ||
| print( | ||
| f"Version ID: {blob.generation}, Soft Delete Time: {blob.soft_delete_time}" | ||
| ) | ||
|
|
||
|
|
||
| # [END storage_list_soft_deleted_object_versions] | ||
|
|
||
| if __name__ == "__main__": | ||
| list_soft_deleted_object_versions(bucket_name=sys.argv[1], blob_name=sys.argv[2]) |
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,40 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2025 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_list_soft_deleted_objects] | ||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def list_soft_deleted_objects(bucket_name): | ||
| """Lists all soft-deleted objects in the bucket.""" | ||
| # bucket_name = "your-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| blobs = storage_client.list_blobs(bucket_name, soft_deleted=True) | ||
|
|
||
| # Note: The call returns a response only when the iterator is consumed. | ||
| for blob in blobs: | ||
| print( | ||
| f"Name: {blob.name}, Generation: {blob.generation}, Soft Delete Time: {blob.soft_delete_time}" | ||
| ) | ||
|
|
||
|
|
||
| # [END storage_list_soft_deleted_objects] | ||
|
|
||
| if __name__ == "__main__": | ||
| list_soft_deleted_objects(bucket_name=sys.argv[1]) |
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.