-
Notifications
You must be signed in to change notification settings - Fork 172
samples: Add samples for soft_deleted_buckets #1463
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
38e8863
samples: Add samples for soft_deleted_buckets
chandra-siri 4f8280c
fix: fix linting errors
chandra-siri 980e9a2
fix: fix linting errors on #1455 - attempt2
chandra-siri 087cf7e
fix: fix linting errors on #1455 - attempt3
chandra-siri 0aaa8b0
fix: test_list_buckets errors
chandra-siri 20a6ad4
fix: address comments by @JesseLovelace
chandra-siri 26b1ca8
samples: Add storage_list_soft_deleted_buckets.py sample and test cas…
chandra-siri 948aa55
fix: lint errors space b/w methods.
chandra-siri d514115
fix: lint issues.
chandra-siri 9c39986
fix: undo changes in storage_list_buckets.py
chandra-siri 6a278eb
fix: lint errors
chandra-siri dacd91a
Change copyright statement.
chandra-siri bc04e62
fix minor typos in doc strings as per code comment
chandra-siri 9bf5616
Merge branch 'main' into 1455-samples-for-bucket-restore
chandra-siri 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,48 @@ | ||
| #!/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_deleted_bucket] | ||
|
|
||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def get_soft_deleted_bucket(bucket_name, generation): | ||
| """Prints out a soft-deleted bucket's metadata. | ||
|
|
||
| Args: | ||
| bucket_name: str | ||
| The name of the bucket to get. | ||
|
|
||
| generation: | ||
| The generation of the bucket. | ||
|
|
||
| """ | ||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name, soft_deleted=True, generation=generation) | ||
|
|
||
| print(f"ID: {bucket.id}") | ||
| print(f"Name: {bucket.name}") | ||
| print(f"Soft Delete time: {bucket.soft_delete_time}") | ||
| print(f"Hard Delete Time : {bucket.hard_delete_time}") | ||
|
|
||
|
|
||
| # [END storage_get_soft_deleted_bucket] | ||
|
|
||
| if __name__ == "__main__": | ||
| get_soft_deleted_bucket(bucket_name=sys.argv[1], generation=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,36 @@ | ||
| #!/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. | ||
|
|
||
| # [START storage_list_soft_deleted_buckets] | ||
|
|
||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def list_soft_deleted_buckets(): | ||
| """Lists all soft-deleted buckets.""" | ||
|
|
||
| storage_client = storage.Client() | ||
| buckets = storage_client.list_buckets(soft_deleted=True) | ||
|
|
||
| for bucket in buckets: | ||
| print(bucket.name) | ||
|
|
||
|
|
||
| # [END storage_list_soft_deleted_buckets] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| list_soft_deleted_buckets() |
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,38 @@ | ||
| #!/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_restore_soft_deleted_bucket] | ||
|
|
||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def restore_bucket(bucket_name, bucket_generation): | ||
| storage_client = storage.Client() | ||
| bucket = storage_client.restore_bucket(bucket_name=bucket_name, generation=bucket_generation) | ||
| print(f"Soft-deleted bucket {bucket.name} with ID: {bucket.id} was restored.") | ||
| print(f"Bucket Generation: {bucket.generation}") | ||
|
|
||
|
|
||
| # [END storage_restore_soft_deleted_bucket] | ||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) != 3: | ||
| print("Wrong inputs!! Usage of script - \"python storage_restore_soft_deleted_bucket.py <bucket_name> <bucket_generation>\" ") | ||
| sys.exit(1) | ||
| restore_bucket(bucket_name=sys.argv[1], bucket_generation=sys.argv[2]) | ||
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.