2222 from collections .abc import AsyncGenerator , Coroutine , Iterable
2323 from typing import Any
2424
25- from obstore import ListStream , ObjectMeta , OffsetRange , SuffixRange
25+ from obstore import ListResult , ListStream , ObjectMeta , OffsetRange , SuffixRange
2626 from obstore .store import ObjectStore as _ObjectStore
2727
2828 from zarr .core .buffer import Buffer , BufferPrototype
@@ -168,8 +168,8 @@ def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
168168 return _transform_list (objects )
169169
170170 def list_dir (self , prefix : str ) -> AsyncGenerator [str , None ]:
171- objects : ListStream [ list [ ObjectMeta ]] = obs .list (self .store , prefix = prefix )
172- return _transform_list_dir (objects , prefix )
171+ coroutine = obs .list_with_delimiter_async (self .store , prefix = prefix )
172+ return _transform_list_dir (coroutine , prefix )
173173
174174
175175async def _transform_list (
@@ -180,19 +180,23 @@ async def _transform_list(
180180 yield item ["path" ]
181181
182182
183+ # ['zarr.json', 'c/0', 'c/1'] -> ['zarr.json']
184+ # ['zarr.json', 'c/0', 'c/1'] -> ['zarr.json, 'c']
183185async def _transform_list_dir (
184- list_stream : AsyncGenerator [ list [ ObjectMeta ], None ], prefix : str
186+ list_result_coroutine : Coroutine [ Any , Any , ListResult ], prefix : str
185187) -> AsyncGenerator [str , None ]:
188+ """
189+ Transform the result of list_with_delimiter into an async generator of prefixes and paths.
190+ """
191+ list_result = await list_result_coroutine
192+
186193 # We assume that the underlying object-store implementation correctly handles the
187194 # prefix, so we don't double-check that the returned results actually start with the
188195 # given prefix.
189- prefix_len = len (prefix ) + 1 # If one is not added to the length, all items will contain "/"
190- async for batch in list_stream :
191- for item in batch :
192- # Yield this item if "/" does not exist after the prefix
193- item_path = item ["path" ][prefix_len :]
194- if "/" not in item_path :
195- yield item_path
196+ prefixes = list_result ["common_prefixes" ]
197+ objects = [obj ["path" ].lstrip (prefix ) for obj in list_result ["objects" ]]
198+ for item in prefixes + objects :
199+ yield item
196200
197201
198202class _BoundedRequest (TypedDict ):
0 commit comments