diff --git a/google/cloud/storage/client.py b/google/cloud/storage/client.py index 74b6061c0..5b9db4348 100644 --- a/google/cloud/storage/client.py +++ b/google/cloud/storage/client.py @@ -64,6 +64,22 @@ _marker = object() +def _buckets_page_start(iterator, page, response): + """Grab prefixes after a :class:`~google.cloud.iterator.Page` started. + + :type iterator: :class:`~google.api_core.page_iterator.Iterator` + :param iterator: The iterator that is currently in use. + + :type page: :class:`~google.cloud.api.core.page_iterator.Page` + :param page: The page that was just created. + + :type response: dict + :param response: The JSON API response for a page of blobs. + """ + page.unreachable = tuple(response.get("unreachable", ())) + iterator.unreachable.update(page.unreachable) + + class Client(ClientWithProject): """Client to bundle configuration needed for API requests. @@ -1551,7 +1567,7 @@ def list_buckets( if soft_deleted is not None: extra_params["softDeleted"] = soft_deleted - return self._list_resource( + iterator = self._list_resource( "/b", _item_to_bucket, page_token=page_token, @@ -1560,7 +1576,10 @@ def list_buckets( page_size=page_size, timeout=timeout, retry=retry, + page_start=_buckets_page_start, ) + iterator.unreachable = set() + return iterator def restore_bucket( self, diff --git a/samples/snippets/storage_list_buckets.py b/samples/snippets/storage_list_buckets.py index f5897e47a..4e6922a80 100644 --- a/samples/snippets/storage_list_buckets.py +++ b/samples/snippets/storage_list_buckets.py @@ -22,9 +22,13 @@ def list_buckets(): """Lists all buckets.""" storage_client = storage.Client() - buckets = storage_client.list_buckets() + buckets_iterator = storage_client.list_buckets() + if hasattr(buckets_iterator, "unreachable"): + print("Unreachable locations:", len(buckets_iterator.unreachable)) + for location in buckets_iterator.unreachable: + print(location) - for bucket in buckets: + for bucket in buckets_iterator: print(bucket.name)