python - 使用 Python 列出具有特定名称类型的 Azure 容器

标签 python azure azure-blob-storage

我试图列出一堆具有特定名称类型的 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/

相关文章:

python - 如何修复 Flask 中的 "The method is not allowed for the requested URL"

azure - 停止 Azure Function 记录 “Executing” 和 “Executed” 消息

reactjs - 将应用程序 react 到 Azure SignalR - FailedWritingMessageToServiceException : Unable to write message to endpoint: https://xxx-dev. service.signalr.net/

c# - 同一存储的依赖注入(inject)多个接口(interface)(具有不同的connectionString)

azure - Microsoft Azure 流分析和 Blob 存储输入

file-upload - 将文件从 Blazor WebAssembly 应用程序直接上传到 Blob 存储

python - 替换 NumPy 数组的某些给定索引的最有效方法是什么?

python - 在 apache 服务器上部署 Django

python - 通过使用 Axes.pie 的 inset_axes 隐藏的 Cartopy coaSTLines

c# - 将 Azure 仪表板集成到自定义 Web 应用程序中