Skip to content

Commit d4b27cb

Browse files
authored
[Storage] Fix #26567: az storage blob download-batch: When matching --pattern, list blobs with prefix to reduce the number of list calls
1 parent c9af1d7 commit d4b27cb

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

  • src/azure-cli/azure/cli/command_modules/storage

src/azure-cli/azure/cli/command_modules/storage/util.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def collect_blob_objects(blob_service, container, pattern=None):
3838
blobs = blob_service.list_blobs(container)
3939
else:
4040
container_client = blob_service.get_container_client(container=container)
41-
blobs = container_client.list_blobs()
41+
prefix = _get_prefix(pattern)
42+
if prefix:
43+
blobs = container_client.list_blobs(name_starts_with=prefix)
44+
else:
45+
blobs = container_client.list_blobs()
4246
for blob in blobs:
4347
try:
4448
blob_name = blob.name.encode('utf-8') if isinstance(blob.name, unicode) else blob.name
@@ -259,6 +263,19 @@ def _pattern_has_wildcards(p):
259263
return not p or p.find('*') != -1 or p.find('?') != -1 or p.find('[') != -1
260264

261265

266+
def _get_prefix(p):
267+
if not p:
268+
return p
269+
pattern_start = len(p)
270+
for index, ch in enumerate(p):
271+
if ch == '*' or ch == '?' or ch == '[':
272+
pattern_start = index
273+
break
274+
if pattern_start == len(p):
275+
return None
276+
return p[:pattern_start]
277+
278+
262279
def _match_path(path, pattern):
263280
from fnmatch import fnmatch
264281
return fnmatch(path, pattern)

0 commit comments

Comments
 (0)