Skip to content

There should be a built-in way to list prefixes (directories) in a bucket #294

@lrowe

Description

@lrowe

The REST API provides a way to list prefixes (directories) in a bucket but this does not seem to be supported by the client library.

According to #192 the iterator returned by list_blobs has a prefixes field which is filled in as you iterate over the blob. This should be better documented, since it is only mentioned in passing:

include_trailing_delimiter (boolean) – (Optional) If true, objects that end in exactly one instance of delimiter will have their metadata included in items in addition to prefixes.

https://googleapis.dev/python/storage/latest/client.html#google.cloud.storage.client.Client.list_blobs

As well as a method to list prefixes alone, it would be helpful to have an iterator that returned both prefixes and objects in order for produce ordered listings.

Workaround found on Stack Overflow. https://stackoverflow.com/a/59008580

def list_prefixes(client, bucket_name, prefix, delimiter):
    # Adapted from https://stackoverflow.com/a/59008580
    from google.api_core import page_iterator
    return page_iterator.HTTPIterator(
        client=client,
        api_request=client._connection.api_request,
        path=f"/b/{bucket_name}/o",
        items_key="prefixes",
        item_to_value=lambda iterator, item: item,
        extra_params={
            "projection": "noAcl",
            "prefix": prefix,
            "delimiter": delimiter,
        },
    )

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/python-storage API.type: cleanupAn internal cleanup or hygiene concern.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions