Python Azure SDK:使用list_blobs获得超过5.000个结果

use*_*680 5 python azure azure-storage-blobs

我在使用Python Azure SDK时遇到问题,在Stack Overflow和Msdn论坛上都找不到任何东西。

我想使用Azure SDKs list_blobs()来获取blob列表-超过5.000(这是max_result)。

如果我看一下SDK本身中的代码,则会看到以下内容:

    def list_blobs(self, container_name, prefix=None, marker=None,
                   maxresults=None, include=None, delimiter=None):
Run Code Online (Sandbox Code Playgroud)

“标记”的说明为:

    marker:
        Optional. A string value that identifies the portion of the list
        to be returned with the next list operation. The operation returns
        a marker value within the response body if the list returned was
        not complete. The marker value may then be used in a subsequent
        call to request the next set of list items. The marker value is
        opaque to the client.
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何使用标记来获取下一组5.000结果。如果我尝试这样的事情:

    blobs = blobservice.list_blobs(target_container, prefix= prefix)            
    print(blobs.marker)
Run Code Online (Sandbox Code Playgroud)

那么该标记始终为空,我认为这是因为list_blobs()已经从响应中解析出了斑点。

但是如果是这种情况,那么我该如何实际地以有意义的方式使用标记?

很抱歉,如果这是一个愚蠢的问题,但这实际上是我没有找到答案的第一个问题,即使经过大量搜索也是如此。

干杯!

Gau*_*tri 5

SDK 在名为 的变量中返回延续令牌next_marker。您应该使用它来获取下一组 blob。请参阅下面的代码作为示例。在这里,我一次从一个容器中列出 100 个 blob:

from azure import *
from azure.storage import *

blob_service = BlobService(account_name='<accountname>', account_key='<accountkey>')
next_marker = None
while True:
    blobs = blob_service.list_blobs('<containername>', maxresults=100, marker=next_marker)
    next_marker = blobs.next_marker
    print(next_marker)
    print(len(blobs))
    if next_marker is None:
        break
print "done"
Run Code Online (Sandbox Code Playgroud)

PS 上面的代码在最后一次迭代时抛出异常。不知道为什么。但它应该给你一个想法。

  • 为了消除此错误,您需要将行 `if next_marker is None:` 更改为 `if not next_marker:`。 (3认同)
  • 不要难过:) ...我最终修改了这里的代码 https://github.com/Azure/azure-sdk-for-python/blob/master/azure/storage/__init__.py#L381 来看看返回的属性。我必须说不是很明显。为了将来参考,您可以在此处查看 SDK 的源代码:https://github.com/Azure/azure-sdk-for-python。哈。 (2认同)