Created
December 17, 2020 19:32
-
-
Save munro/8256b0c40cee3c929501478f1294e693 to your computer and use it in GitHub Desktop.
Google Cloud Storage List Directories in Python
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
| def gcs_list_folders(bucket, prefix="", delimeter="/", guess_lexicographically_last_item="~", gcs_client=None): | |
| folders = set() | |
| prefix_parts = prefix.split(delimeter) | |
| start_offset = "/".join(prefix_parts) | |
| last_blob_name = None | |
| while True: | |
| blobs = list(gcs_client.list_blobs( | |
| bucket_or_name=bucket, | |
| prefix=prefix, | |
| start_offset=start_offset, | |
| max_results=1 | |
| )) | |
| if not blobs: | |
| break | |
| blob = blobs[0] | |
| if last_blob_name == blob.name: | |
| raise Exception("Saw blob {} twice, try setting a different guess_lexicographically_last_item={}.".format( | |
| repr(blob.name), repr(guess_lexicographically_last_item) | |
| )) | |
| folder = delimeter.join(blob.name.split(delimeter)[0:len(prefix_parts)] + [""]) | |
| folders.add(folder) | |
| start_offset = folder + guess_lexicographically_last_item | |
| last_blob_name = blob.name | |
| try_characters = 1 | |
| return folders | |
| from google.cloud.storage.client import Client | |
| gcs_client = Client() | |
| print(gcs_list_folders(bucket="my-bucket", gcs_client=gcs_client)) | |
| print(gcs_list_folders(bucket="my-bucket", prefix="foo/", gcs_client=gcs_client)) | |
| print(gcs_list_folders(bucket="my-bucket", prefix="foo/bar/", gcs_client=gcs_client)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment