Python Azure SDK:使用list_blobs获取超过5000个结果
我在使用Python的Azure SDK时遇到了一些问题,在Stack Overflow和Msdn论坛上都没有找到相关的信息。
我想用Azure SDK里的list_blobs()方法来获取一个blob的列表,但这些blob的数量超过了5000个(这是最大结果数)。
如果我查看SDK里的代码,我看到如下内容:
def list_blobs(self, container_name, prefix=None, marker=None,
maxresults=None, include=None, delimiter=None):
关于'标记'的描述是:
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.
我的问题是,我不知道如何使用这个标记来获取下一组5000个结果。如果我尝试像这样:
blobs = blobservice.list_blobs(target_container, prefix= prefix)
print(blobs.marker)
那么标记总是为空,我猜这是因为list_blobs()已经从响应中解析出了blob。
但如果是这样的话,我到底该如何有效地使用这个标记呢?
抱歉如果这个问题听起来很傻,但这是我第一次找不到答案,尽管我已经搜索了很久。
谢谢!
2 个回答
0
在编程中,有时候我们会遇到一些问题,像是代码运行不正常或者出现错误。这种情况下,我们需要去寻找解决办法。StackOverflow是一个很棒的地方,很多程序员会在这里分享他们的经验和解决方案。
当你在StackOverflow上提问时,记得描述清楚你的问题,提供相关的代码和错误信息,这样其他人才能更好地帮助你。通常,社区里的成员会根据他们的经验给出建议,或者指引你找到解决问题的方法。
总之,StackOverflow是一个学习和解决问题的好地方,利用好这个资源,可以让你的编程之路更加顺利。
from azure import *
from azure.storage import *
block_blob_service = BlockBlobService(account_name=AccountName, account_key=AccountKey)
names= []
next_marker = None
while True:
blob_list = block_blob_service.list_blobs(container, prefix='/curated/projects/IntelligentShipmentOptimization/',
num_results=400, marker=next_marker)
for blob in blob_list:
names.append(blob.name)
next_marker = blob_list.next_marker
print(next_marker)
if not next_marker:
break
5
SDK会返回一个叫做 next_marker
的变量,这个变量里存着继续获取数据的标记。你可以用这个标记来获取下一组数据。下面的代码就是一个例子。在这里,我一次从一个容器里列出了100个数据:
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"
附注:上面的代码在最后一次循环时会抛出一个异常。我也不太清楚为什么会这样。不过这段代码应该能让你明白大概的意思。