【问题标题】:Azure Functions - Python (Blob Trigger and Binding)Azure Functions - Python(Blob 触发器和绑定)
【发布时间】:2021-12-31 01:56:52
【问题描述】:

我已查看 Microsoft 提供的有关触发器的文档。 [https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=python][1]

确实,在 Azure 函数中使用 func.InputStream 参数允许我们检索 blob 和一些属性 (name, uri, length),我们还可以使用 read() 函数读取字节,但是我们如何将字节转换为我们可以操作的对象,例如 Pandas 数据框(或其他类型文件的任何其他类型的对象,例如 jpg)?

我的 host.json 文件可以在下面找到:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "statscan/raw/ncdb/{name}",
      "connection": ""
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "outputBlob",
      "path": "statscan/enriched/func/{name}.csv",
      "connection": ""
    }
  ]
}

Blob Trigger 函数可以在下面找到:

import pandas as pd
import logging
import azure.functions as func

def main(myblob: func.InputStream, outputBlob: func.Out[str]):

    logging.info(f"Blob trigger executed!")
    logging.info(f"Blob Name: {myblob.name} ({myblob.length}) bytes")
    logging.info(f"Full Blob URI: {myblob.uri}")

    ### Manipulate with Pandas ###

    ### Output ###
    output = ''
    outputBlob.set(output)

【问题讨论】:

    标签: python azure function


    【解决方案1】:

    我们有多种方法来检查文件内容并相应地读取它,在您的情况下,让我们将 csv 格式视为 blob。

    • 为了实现这一点,我们可以下载 blob,然后将数据读取到数据帧,以下是我从 MS Docs 尝试的方法(https://docs.microsoft.com/en-us/azure/architecture/data-science-process/explore-data-blob):

        from azure.storage.blob import BlobServiceClient
        import pandas as pd
      
        STORAGEACCOUNTURL= <storage_account_url>
        STORAGEACCOUNTKEY= <storage_account_key>
        LOCALFILENAME= <local_file_name>
        CONTAINERNAME= <container_name>
        BLOBNAME= <blob_name>
      
        #download from blob
        t1=time.time()
        blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
        blob_client_instance = blob_service_client_instance.get_blob_client(CONTAINERNAME, BLOBNAME, snapshot=None)
        with open(LOCALFILENAME, "wb") as my_blob:
            blob_data = blob_client_instance.download_blob()
            blob_data.readinto(my_blob)
        t2=time.time()
        print(("It takes %s seconds to download "+BLOBNAME) % (t2 - t1))
      
    • 其他我们可以直接用blob sas url转换如下:

        from io import StringIO
        blobstring = blob_service.get_blob_to_text(CONTAINERNAME,BLOBNAME).content
        df = pd.read_csv(StringIO(blobstring))
      
    • 另一种方式是使用blob sas url,我们可以在blob上右键选择“Generate SAS”来获取:

          import pandas as pd
          data = pd.read_csv('blob_sas_url')
      
      
      

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 2020-05-07
      • 2018-08-29
      • 1970-01-01
      • 2017-04-06
      • 2017-04-21
      • 2022-01-19
      • 2018-10-26
      相关资源
      最近更新 更多