From 38e8863ebb62aacb3c090bf4088e203be4c94845 Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Wed, 16 Apr 2025 09:09:46 +0000 Subject: [PATCH 01/13] samples: Add samples for soft_deleted_buckets --- .../storage_get_soft_deleted_bucket.py | 47 +++++++++++++++++++ samples/snippets/storage_list_buckets.py | 10 ++-- .../storage_restore_soft_deleted_bucket.py | 38 +++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 samples/snippets/storage_get_soft_deleted_bucket.py create mode 100644 samples/snippets/storage_restore_soft_deleted_bucket.py diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py new file mode 100644 index 000000000..7740a937b --- /dev/null +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +# Copyright 2019 Google Inc. All Rights Reserved. +# +# 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-delted bucket's metadata. + + Args: + bucket: str + The bucket resource to pass or name to create. + + 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}") + + +if __name__ == "__main__": + get_soft_deleted_bucket(bucket_name=sys.argv[1], generation=sys.argv[2]) + +# [END storage_get_soft_deleted_bucket] \ No newline at end of file diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index f5897e47a..a53584817 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -15,14 +15,15 @@ # limitations under the License. # [START storage_list_buckets] +import sys from google.cloud import storage -def list_buckets(): +def list_buckets(soft_deleted=False): """Lists all buckets.""" storage_client = storage.Client() - buckets = storage_client.list_buckets() + buckets = storage_client.list_buckets(soft_deleted=soft_deleted, max_results=10) for bucket in buckets: print(bucket.name) @@ -32,4 +33,7 @@ def list_buckets(): if __name__ == "__main__": - list_buckets() + if len(sys.argv) == 2 and (sys.argv[1] == 'True' or sys.argv[1] == 'False'): + list_buckets(soft_deleted=sys.argv[1]) + else: + list_buckets() diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py new file mode 100644 index 000000000..dbc852717 --- /dev/null +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +# Copyright 2019 Google Inc. All Rights Reserved. +# +# 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"ID: {bucket.id}") + print(f"Name: {bucket.name}") + print(f"Bucket Generation: {bucket.generation}") + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Wrong inputs!! Usage of script - \"python storage_restore_soft_deleted_bucket.py \" ") + sys.exit(1) + restore_bucket(bucket_name=sys.argv[1], bucket_generation=sys.argv[2]) + +# [END storage_restore_soft_deleted_bucket] \ No newline at end of file From 4f8280c7b4a84b0cb56d07edc8b5e6d1a97eac2a Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Wed, 16 Apr 2025 09:37:39 +0000 Subject: [PATCH 02/13] fix: fix linting errors --- samples/snippets/storage_get_soft_deleted_bucket.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index 7740a937b..d483088b6 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -21,6 +21,7 @@ from google.cloud import storage + def get_soft_deleted_bucket(bucket_name, generation): """Prints out a soft-delted bucket's metadata. @@ -30,7 +31,7 @@ def get_soft_deleted_bucket(bucket_name, generation): generation: The generation of the bucket. - + """ storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name, soft_deleted=True, generation=generation) @@ -44,4 +45,4 @@ def get_soft_deleted_bucket(bucket_name, generation): if __name__ == "__main__": get_soft_deleted_bucket(bucket_name=sys.argv[1], generation=sys.argv[2]) -# [END storage_get_soft_deleted_bucket] \ No newline at end of file +# [END storage_get_soft_deleted_bucket] From 980e9a2d06d5d7d5546996cac5f621f66bcc349a Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Wed, 16 Apr 2025 10:28:39 +0000 Subject: [PATCH 03/13] fix: fix linting errors on #1455 - attempt2 --- samples/snippets/storage_get_soft_deleted_bucket.py | 3 +-- samples/snippets/storage_list_buckets.py | 4 +++- samples/snippets/storage_restore_soft_deleted_bucket.py | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index d483088b6..e4c4dd33e 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -41,8 +41,7 @@ def get_soft_deleted_bucket(bucket_name, generation): 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]) - -# [END storage_get_soft_deleted_bucket] diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index a53584817..4268a01e2 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -14,8 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START storage_list_buckets] import sys + +# [START storage_list_buckets] + from google.cloud import storage diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py index dbc852717..dc5720569 100644 --- a/samples/snippets/storage_restore_soft_deleted_bucket.py +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -21,6 +21,7 @@ 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) @@ -28,6 +29,7 @@ def restore_bucket(bucket_name, bucket_generation): print(f"Name: {bucket.name}") print(f"Bucket Generation: {bucket.generation}") +# [END storage_restore_soft_deleted_bucket] if __name__ == "__main__": if len(sys.argv) != 3: @@ -35,4 +37,4 @@ def restore_bucket(bucket_name, bucket_generation): sys.exit(1) restore_bucket(bucket_name=sys.argv[1], bucket_generation=sys.argv[2]) -# [END storage_restore_soft_deleted_bucket] \ No newline at end of file + From 087cf7ec74ddac7d1f3da33f1dd9658c69e211e0 Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Wed, 16 Apr 2025 10:34:16 +0000 Subject: [PATCH 04/13] fix: fix linting errors on #1455 - attempt3 --- samples/snippets/storage_get_soft_deleted_bucket.py | 1 + samples/snippets/storage_restore_soft_deleted_bucket.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index e4c4dd33e..11e3f7a54 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -41,6 +41,7 @@ def get_soft_deleted_bucket(bucket_name, generation): 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__": diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py index dc5720569..63a33fa7d 100644 --- a/samples/snippets/storage_restore_soft_deleted_bucket.py +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -29,6 +29,7 @@ def restore_bucket(bucket_name, bucket_generation): print(f"Name: {bucket.name}") print(f"Bucket Generation: {bucket.generation}") + # [END storage_restore_soft_deleted_bucket] if __name__ == "__main__": @@ -36,5 +37,3 @@ def restore_bucket(bucket_name, bucket_generation): print("Wrong inputs!! Usage of script - \"python storage_restore_soft_deleted_bucket.py \" ") sys.exit(1) restore_bucket(bucket_name=sys.argv[1], bucket_generation=sys.argv[2]) - - From 0aaa8b0bcceeeddbb2fc3ec78817f2f59d7cf25a Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Wed, 16 Apr 2025 10:52:40 +0000 Subject: [PATCH 05/13] fix: test_list_buckets errors --- samples/snippets/storage_list_buckets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index 4268a01e2..5cd440d66 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -25,7 +25,7 @@ def list_buckets(soft_deleted=False): """Lists all buckets.""" storage_client = storage.Client() - buckets = storage_client.list_buckets(soft_deleted=soft_deleted, max_results=10) + buckets = storage_client.list_buckets(soft_deleted=soft_deleted) for bucket in buckets: print(bucket.name) From 20a6ad452bb621e8fd87092ba1ac8c27dba7942b Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 04:13:06 +0000 Subject: [PATCH 06/13] fix: address comments by @JesseLovelace --- .../storage_get_soft_deleted_bucket.py | 4 +-- samples/snippets/storage_list_buckets.py | 25 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index 11e3f7a54..b26467018 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2019 Google Inc. All Rights Reserved. +# Copyright 2025 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. @@ -27,7 +27,7 @@ def get_soft_deleted_bucket(bucket_name, generation): Args: bucket: str - The bucket resource to pass or name to create. + The name of the bucket to get. generation: The generation of the bucket. diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index 5cd440d66..d245a59dd 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -16,26 +16,27 @@ import sys -# [START storage_list_buckets] - +# [START storage_list_files] from google.cloud import storage -def list_buckets(soft_deleted=False): - """Lists all buckets.""" +def list_blobs(bucket_name): + """Lists all the blobs in the bucket.""" + # bucket_name = "your-bucket-name" storage_client = storage.Client() - buckets = storage_client.list_buckets(soft_deleted=soft_deleted) - for bucket in buckets: - print(bucket.name) + # Note: Client.list_blobs requires at least package version 1.17.0. + blobs = storage_client.list_blobs(bucket_name) + + # Note: The call returns a response only when the iterator is consumed. + for blob in blobs: + print(blob.name) -# [END storage_list_buckets] +# [END storage_list_files] if __name__ == "__main__": - if len(sys.argv) == 2 and (sys.argv[1] == 'True' or sys.argv[1] == 'False'): - list_buckets(soft_deleted=sys.argv[1]) - else: - list_buckets() + list_blobs(bucket_name=sys.argv[1]) + From 26b1ca8a0fb4972a2a22add76e8f2ab83c6af4cc Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 10:06:41 +0000 Subject: [PATCH 07/13] samples: Add storage_list_soft_deleted_buckets.py sample and test cases for all --- samples/snippets/snippets_test.py | 30 +++++++++++++++ samples/snippets/storage_list_buckets.py | 1 - .../storage_list_soft_deleted_buckets.py | 38 +++++++++++++++++++ .../storage_restore_soft_deleted_bucket.py | 2 +- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/storage_list_soft_deleted_buckets.py diff --git a/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index 339693dd8..9e1d5b25a 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/snippets_test.py @@ -58,9 +58,12 @@ import storage_get_autoclass import storage_get_bucket_labels import storage_get_bucket_metadata +import storage_get_soft_deleted_bucket import storage_get_metadata import storage_get_service_account import storage_list_buckets +import storage_list_soft_deleted_buckets +import storage_restore_soft_deleted_bucket import storage_list_file_archived_generations import storage_list_files import storage_list_files_with_prefix @@ -129,6 +132,18 @@ def test_bucket(): yield bucket bucket.delete(force=True) +@pytest.fixture(scope="module") +def test_soft_deleted_bucket(): + """Yields a soft-deleted bucket.""" + bucket = None + while bucket is None or bucket.exists(): + bucket_name = f"storage-snippets-test-{uuid.uuid4()}" + bucket = storage.Client().bucket(bucket_name) + bucket.create() + # [Assumption] Bucket is created with default policy , ie soft delete on. + bucket.delete() + yield bucket + @pytest.fixture(scope="function") def test_public_bucket(): @@ -194,6 +209,11 @@ def test_list_buckets(test_bucket, capsys): assert test_bucket.name in out +def test_list_soft_deleted_buckets(test_soft_deleted_bucket, capsys): + storage_list_soft_deleted_buckets.list_soft_deleted_buckets() + out, _ = capsys.readouterr() + assert test_soft_deleted_bucket.name in out + def test_list_blobs(test_blob, capsys): storage_list_files.list_blobs(test_blob.bucket.name) out, _ = capsys.readouterr() @@ -205,6 +225,16 @@ def test_bucket_metadata(test_bucket, capsys): out, _ = capsys.readouterr() assert test_bucket.name in out +def test_get_soft_deleted_bucket(test_soft_deleted_bucket, capsys): + storage_get_soft_deleted_bucket.get_soft_deleted_bucket(test_soft_deleted_bucket.name, test_soft_deleted_bucket.generation) + out, _ = capsys.readouterr() + assert test_soft_deleted_bucket.name in out + +def test_restore_soft_deleted_bucket(test_soft_deleted_bucket, capsys): + storage_restore_soft_deleted_bucket.restore_bucket(test_soft_deleted_bucket.name, test_soft_deleted_bucket.generation) + out, _ = capsys.readouterr() + assert test_soft_deleted_bucket.name in out + def test_list_blobs_with_prefix(test_blob, capsys): storage_list_files_with_prefix.list_blobs_with_prefix( diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index d245a59dd..5e80c833a 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -39,4 +39,3 @@ def list_blobs(bucket_name): if __name__ == "__main__": list_blobs(bucket_name=sys.argv[1]) - diff --git a/samples/snippets/storage_list_soft_deleted_buckets.py b/samples/snippets/storage_list_soft_deleted_buckets.py new file mode 100644 index 000000000..2d991cb64 --- /dev/null +++ b/samples/snippets/storage_list_soft_deleted_buckets.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +# Copyright 2025 Google Inc. All Rights Reserved. +# +# 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_buckets] + +from google.cloud import storage + + +def list_soft_deleted_buckets(): + """Lists all 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() \ No newline at end of file diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py index 63a33fa7d..77a751e3a 100644 --- a/samples/snippets/storage_restore_soft_deleted_bucket.py +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2019 Google Inc. All Rights Reserved. +# Copyright 2025 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. From 948aa5555b847e04442b959391773613bba59fa1 Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 10:14:57 +0000 Subject: [PATCH 08/13] fix: lint errors space b/w methods. --- samples/snippets/snippets_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index 9e1d5b25a..6b754d343 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/snippets_test.py @@ -214,6 +214,7 @@ def test_list_soft_deleted_buckets(test_soft_deleted_bucket, capsys): out, _ = capsys.readouterr() assert test_soft_deleted_bucket.name in out + def test_list_blobs(test_blob, capsys): storage_list_files.list_blobs(test_blob.bucket.name) out, _ = capsys.readouterr() @@ -225,11 +226,13 @@ def test_bucket_metadata(test_bucket, capsys): out, _ = capsys.readouterr() assert test_bucket.name in out + def test_get_soft_deleted_bucket(test_soft_deleted_bucket, capsys): storage_get_soft_deleted_bucket.get_soft_deleted_bucket(test_soft_deleted_bucket.name, test_soft_deleted_bucket.generation) out, _ = capsys.readouterr() assert test_soft_deleted_bucket.name in out + def test_restore_soft_deleted_bucket(test_soft_deleted_bucket, capsys): storage_restore_soft_deleted_bucket.restore_bucket(test_soft_deleted_bucket.name, test_soft_deleted_bucket.generation) out, _ = capsys.readouterr() From d5141150d99e76a12ea1f28673a8d4893a93ab9b Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 10:17:23 +0000 Subject: [PATCH 09/13] fix: lint issues. --- samples/snippets/snippets_test.py | 1 + samples/snippets/storage_list_soft_deleted_buckets.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index 6b754d343..5a9321ba0 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/snippets_test.py @@ -132,6 +132,7 @@ def test_bucket(): yield bucket bucket.delete(force=True) + @pytest.fixture(scope="module") def test_soft_deleted_bucket(): """Yields a soft-deleted bucket.""" diff --git a/samples/snippets/storage_list_soft_deleted_buckets.py b/samples/snippets/storage_list_soft_deleted_buckets.py index 2d991cb64..99a25361a 100644 --- a/samples/snippets/storage_list_soft_deleted_buckets.py +++ b/samples/snippets/storage_list_soft_deleted_buckets.py @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys - # [START storage_list_soft_deleted_buckets] from google.cloud import storage @@ -35,4 +33,4 @@ def list_soft_deleted_buckets(): if __name__ == "__main__": - list_soft_deleted_buckets() \ No newline at end of file + list_soft_deleted_buckets() From 9c39986eb0fcb7724bb642dc006abca35f1eb3f0 Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 10:43:12 +0000 Subject: [PATCH 10/13] fix: undo changes in storage_list_buckets.py --- samples/snippets/storage_list_buckets.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index 5e80c833a..77901d403 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -14,28 +14,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys - -# [START storage_list_files] +# [START storage_list_buckets] from google.cloud import storage -def list_blobs(bucket_name): - """Lists all the blobs in the bucket.""" - # bucket_name = "your-bucket-name" +def list_buckets(): + """Lists all buckets.""" storage_client = storage.Client() + buckets = storage_client.list_buckets() - # Note: Client.list_blobs requires at least package version 1.17.0. - blobs = storage_client.list_blobs(bucket_name) - - # Note: The call returns a response only when the iterator is consumed. - for blob in blobs: - print(blob.name) + for bucket in buckets: + print(bucket.name) -# [END storage_list_files] +# [END storage_list_buckets] if __name__ == "__main__": - list_blobs(bucket_name=sys.argv[1]) + list_buckets() + From 6a278eb86cdcd89feb47da759e2a5c2506d804ea Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Thu, 17 Apr 2025 10:43:47 +0000 Subject: [PATCH 11/13] fix: lint errors --- samples/snippets/storage_list_buckets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index 77901d403..f5897e47a 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -33,4 +33,3 @@ def list_buckets(): if __name__ == "__main__": list_buckets() - From dacd91a30477e8b95b77142e0cc665a10524b1ce Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Mon, 5 May 2025 05:04:35 +0000 Subject: [PATCH 12/13] Change copyright statement. --- samples/snippets/storage_get_soft_deleted_bucket.py | 2 +- samples/snippets/storage_list_soft_deleted_buckets.py | 2 +- samples/snippets/storage_restore_soft_deleted_bucket.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index b26467018..42930753c 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2025 Google Inc. All Rights Reserved. +# 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. diff --git a/samples/snippets/storage_list_soft_deleted_buckets.py b/samples/snippets/storage_list_soft_deleted_buckets.py index 99a25361a..5278ce065 100644 --- a/samples/snippets/storage_list_soft_deleted_buckets.py +++ b/samples/snippets/storage_list_soft_deleted_buckets.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2025 Google Inc. All Rights Reserved. +# 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. diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py index 77a751e3a..4a1a396fc 100644 --- a/samples/snippets/storage_restore_soft_deleted_bucket.py +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2025 Google Inc. All Rights Reserved. +# 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. From bc04e62e51bfbf038a3ccce46809a68c63ca04f7 Mon Sep 17 00:00:00 2001 From: Chandra Sirimala Date: Mon, 5 May 2025 05:22:27 +0000 Subject: [PATCH 13/13] fix minor typos in doc strings as per code comment --- samples/snippets/storage_get_soft_deleted_bucket.py | 4 ++-- samples/snippets/storage_list_soft_deleted_buckets.py | 2 +- samples/snippets/storage_restore_soft_deleted_bucket.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/samples/snippets/storage_get_soft_deleted_bucket.py b/samples/snippets/storage_get_soft_deleted_bucket.py index 42930753c..2b7955046 100644 --- a/samples/snippets/storage_get_soft_deleted_bucket.py +++ b/samples/snippets/storage_get_soft_deleted_bucket.py @@ -23,10 +23,10 @@ def get_soft_deleted_bucket(bucket_name, generation): - """Prints out a soft-delted bucket's metadata. + """Prints out a soft-deleted bucket's metadata. Args: - bucket: str + bucket_name: str The name of the bucket to get. generation: diff --git a/samples/snippets/storage_list_soft_deleted_buckets.py b/samples/snippets/storage_list_soft_deleted_buckets.py index 5278ce065..16abd90f0 100644 --- a/samples/snippets/storage_list_soft_deleted_buckets.py +++ b/samples/snippets/storage_list_soft_deleted_buckets.py @@ -20,7 +20,7 @@ def list_soft_deleted_buckets(): - """Lists all buckets.""" + """Lists all soft-deleted buckets.""" storage_client = storage.Client() buckets = storage_client.list_buckets(soft_deleted=True) diff --git a/samples/snippets/storage_restore_soft_deleted_bucket.py b/samples/snippets/storage_restore_soft_deleted_bucket.py index 4a1a396fc..fb6291997 100644 --- a/samples/snippets/storage_restore_soft_deleted_bucket.py +++ b/samples/snippets/storage_restore_soft_deleted_bucket.py @@ -25,8 +25,7 @@ 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"ID: {bucket.id}") - print(f"Name: {bucket.name}") + print(f"Soft-deleted bucket {bucket.name} with ID: {bucket.id} was restored.") print(f"Bucket Generation: {bucket.generation}")