【问题标题】:Azure python function. Read Blob line by line [duplicate]Azure Python 函数。逐行读取 Blob [重复]
【发布时间】:2021-06-18 21:57:05
【问题描述】:

使用 azure python 函数,我可以一次读取完整的文件。 (Read data from Azure blob storage in Azure Function in 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 文件? 谢谢!

【问题讨论】:

    标签: python azure azure-functions azure-blob-storage


    【解决方案1】:

    您可以尝试使用此代码逐行读取 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)
    
    

    更多参考,您可以查看此链接

    read the text file in azure storage blob line by line using python

    【讨论】:

      【解决方案2】:

      为了完整起见,我使用 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='

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-29
        • 2016-03-21
        • 2019-09-21
        • 1970-01-01
        • 2018-07-30
        • 2021-12-18
        • 2018-01-26
        • 2019-12-11
        相关资源
        最近更新 更多