从前缀为 "abc" 的存储容器中读取文件并加载为 json.. 错误为“'BlobProperties' 对象没有属性 'download_blob'”

Reading file from storage container with prefix "abc" and loading as json.. Errored as " 'BlobProperties' object has no attribute 'download_blob' "

下面是代码,

connection_string='DefaultEndpointsProtocol=https;AccountName=example;AccountKey='
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("example123")
files = container_client.list_blobs(name_starts_with="ABC")
print(files)
for blob in files:
     print("\t" + blob.name)
     blobr = blob.download_blob().readall()
     print(blobr)
     tmp_data = blobr.read().decode('utf-8')
     print(tmp.data)

错误:

AttributeError: 'BlobProperties' object has no attribute 'download_blob'

请指导如何从存储容器中读取 blob 并加载 json 或进行进一步处理

这是由于 list_blobs 方法仅 returns blob 属性,而不是 blob 对象。所以你不能通过 blob 属性.

调用 download_blob 方法

您应该在 for 语句中像下面这样更改您的代码:

for blob in files:
    blob_client=blob_service_client.get_blob_client("like example123, this is your container name",blob.name)
    blobr = blob_client.download_blob().readall()