简体   繁体   English

如何使用 python 有效地列出 Azure blob 中的所有文件?

[英]How to efficiently list all files in an Azure blob using python?

I need to list all files in an Azure blob using python. Currently I use the code below.我需要使用 python 列出 Azure blob 中的所有文件。目前我使用下面的代码。 this worked well when there were few files.当文件很少时,这很有效。 But now I have a large number of files and the script runs more than an hour.但是现在我有大量文件并且脚本运行了一个多小时。 The time-consuming part is the for loop.比较耗时的部分是for循环。 How can this be done faster?如何才能更快地做到这一点?

import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
import pandas as pd

connect_str = "************"

blob_service_client = BlobServiceCliaent.from_connection_string(connect_str)

blob_service_client.get_account_information()
c = blob_service_client.list_containers()

container_client = blob_service_client.get_container_client("blobName")

l = []
for blob in container_client.list_blobs():
    l.append(blob.name)

I could able to achieve this using list_blobs method of BlockBlobService .我可以使用list_blobs BlockBlobService来实现这一点。 After reproducing from my end, I have observed that the list_blobs method of BlobServiceClient returns all the properties of blob which is taking more time to proocess whereas BlockBlobService returns objects.从我这边重现后,我观察到BlobServiceClientlist_blobs方法返回 blob 的所有属性,这需要更多时间来处理,而BlockBlobService返回对象。 Below is the code that was working for me.以下是为我工作的代码。

import os
from azure.storage.blob import BlockBlobService
import datetime

ACCOUNT_NAME = "<YOUR_ACCOUNT_NAME>"
CONTAINER_NAME = "<YOUR_CONTAINER_NAME>"
SAS_TOKEN='<YOUR_SAS_TOKEN>'

block_blob_service = BlockBlobService(account_name=ACCOUNT_NAME,account_key=None,sas_token=SAS_TOKEN)

# Lists All Blobs
l =[]
print("\nList blobs in the container")
generator = block_blob_service.list_blobs(CONTAINER_NAME)
for blob in generator:
    print("a"+str(datetime.datetime.now()))
    blobname=blob
    l.append(blob.name)
    
print(l)
    
print("b"+str(datetime.datetime.now()))

OUTPUT: OUTPUT:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 列出 Azure 存储容器中的所有文件和 blob - Python - List all the files and blob inside an Azure Storage Container 如何使用 Python 从给定 SAS URI 和容器名称的 Azure Blob 存储下载文件列表? - How to download a list of files from Azure Blob Storage given SAS URI and container name using Python? 如何使用 Python 从 azure blob 读取 docx 文件 - How to read docx files from azure blob using Python 如何使用 python 从 blob 容器下载所有文件 - How to download all files from a blob container using python 使用 python azure 函数从 azure blob 存储读取文件 - Read files from azure blob storage using python azure functions 如何使用 Python 在 Azure 中创建 Blob 容器? - How to create a blob container in Azure using Python? 使用 Python 从 azure blob 存储下载文件(csv、excel) - Download files (csv, excel) from azure blob storage using Python Stream 文件到 Azure Blob 存储中的 Zip 文件,使用 ZA7F5F35426B9627411FC3Z231 - Stream Files to Zip File in Azure Blob Storage using Python? 如何使用 PowerShell 或 python 脚本读取然后编辑或附加存储在 Azure Blob 存储中的 Excel 文件(列和行) - How to read then edit or append an Excel Files (columns and rows ) stored in Azure Blob Storage using PowerShell or python script 使用 python 从 Azure blob 读取 Json 文件? - Read Json files from Azure blob using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM