【问题标题】:List azure containers with specific name type using Python使用 Python 列出具有特定名称类型的 azure 容器
【发布时间】:2019-03-29 10:24:01
【问题描述】:

我正在尝试列出一堆具有特定名称类型的 azure 容器 - 它们都称为cycling-asset-group-x,其中 x 是数字或字母,例如循环资产组a,循环资产组1,循环资产组b,循环资产组2。

我只想打印带有 number 后缀的容器,即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)

【问题讨论】:

    标签: python azure azure-blob-storage


    【解决方案1】:

    只需将您的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)[来源]

    参数:
    prefix (str) – 过滤结果以仅返回名称以指定前缀开头的容器。

    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)
    

    【讨论】:

    • 抱歉,彼得,我已将问题更新为更清楚 - 有多个具有此前缀的容器,但后缀不同
    • @Beansy 我已经更新了我的帖子,你可以参考我的新代码。
    • @Beansy 如果我的新答案对您有帮助,您可以标记一下吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多