Azure python 函数。逐行读取 Blob

Azure python function. Read Blob line by line

使用 azure python 函数,我可以一次读取完整文件。 ()

blob_client = BlobClient.from_blob_url("your Blob SAS URL here")
download_stream = blob_client.download_blob()
logging.info('=========below is content of test1')
logging.info(download_stream.readall())
logging.info('=========above is content of test1')

有没有一种方法可以逐行读取 blob 文件? 谢谢!

您可以尝试使用此代码逐行读取 blob 文件


str1 = block_blob_service.get_blob_to_text(container_name,blob_name)

#split the string str1 with newline.
arr1 = str1.content.splitlines()

#read the one line each time.
for a1 in arr1:
    print(a1)

如需更多参考,您可以查看此 link

为了完整起见,我使用 SAS 令牌访问的代码:

from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name='<account_name>', account_key=None,sas_token ='<sas_token>')
str1 = block_blob_service.get_blob_to_text('<container_name>','<blob_name>')
arr1 = str1.content.splitlines()
#read the one line each time.
for a1 in arr1:
    print(a1)

作为一个陷阱,SAS 令牌以没有 ?象征。 SAS 令牌应以类似以下内容开头:'sv='