-
Notifications
You must be signed in to change notification settings - Fork 163
Closed
Labels
api: storageIssues related to the googleapis/python-storage API.Issues related to the googleapis/python-storage API.type: cleanupAn internal cleanup or hygiene concern.An internal cleanup or hygiene concern.type: docsImprovement to the documentation for an API.Improvement to the documentation for an API.
Description
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.
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,
},
)
sandipchatterjee and amirbtb
Metadata
Metadata
Assignees
Labels
api: storageIssues related to the googleapis/python-storage API.Issues related to the googleapis/python-storage API.type: cleanupAn internal cleanup or hygiene concern.An internal cleanup or hygiene concern.type: docsImprovement to the documentation for an API.Improvement to the documentation for an API.