我试图列出一堆具有特定名称类型的 azure 容器 - 它们都称为cycling-asset-group-x,其中x是数字或字母,例如循环 Assets 组 a、循环 Assets 组 1、循环 Assets 组 b、循环 Assets 组 2。
我只想打印后缀带有数字的容器,即cycling-asset-group-1、cycling-asset-group-2等
我该怎么做?这是我到目前为止所做的事情:
account_name = 'name'
account_key = 'key'
# connect to the storage account
blob_service = BaseBlobService(account_name = account_name, account_key = account_key)
prefix_input_container = 'cycling-asset-group-'
# get a list of the containers - I think it's something like this...?
cycling_containers = blob_service.list_containers("%s%d" % (prefix_input_container,...))
for c in cycling_containers:
contname = c.name
print(contname)
最佳答案
只需将您的 prefix_input_container
值传递给 BaseBlobService
的方法 list_containers
的参数 prefix
即可,如下代码以下。请参阅 API 引用 BaseBlobService.list_containers
.
list_containers(prefix=None, num_results=None, include_metadata=False, marker=None, timeout=None)[source]
Parameters:
prefix (str) – Filters the results to return only containers whose names begin with the specified prefix.
prefix_input_container = 'cycling-asset-group-'
cycling_containers = blob_service.list_containers(prefix=prefix_input_container)
# Import regex module to filter the results
import re
re_expression = r"%s\d+$" % prefix_input_container
pattern = re.compile(re_expression)
# There are two ways.
# No.1 Create a generator from the generator of cycling_containers
filtered_cycling_container_names = (c.name for c in cycling_containers if pattern.match(c.name))
for contname in filtered_cycling_container_names:
print(contname)
# No.2 Create a name list
contnames = [c.name for c in cycling_containers if pattern.match(c.name)]
print(contnames)
关于python - 使用 Python 列出具有特定名称类型的 Azure 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55382644/