在python的Azure函数中从Azure blob存储读取数据

时间:2020-11-10 20:07:01

标签: azure-devops azure-functions azure-data-lake azure-blob-storage azure-function-app

请在启动Function应用程序时如何从Azure存储帐户读取数据。我需要在运行时为我的机器学习模型读取保存的权重。 我想直接从存储帐户中读取模型,因为该模型预计每天都会更新,并且不需要手动重新部署该模型。

谢谢

1 个答案:

答案 0 :(得分:0)

为此,您可以先转到存储Blob,然后单击“ 生成SAS ”以生成“ Blob SAS URL ”(也可以定义开始日期以及网址的到期日期)。 enter image description here

然后转到python函数,通过在VS代码中运行azure-storage-blob命令来安装pip install azure-storage-blob模块。之后,编写如下功能代码: enter image description here

启动该函数并触发它,我们可以看到test1.txt打印出的logging.info的内容。 enter image description here

下面是我所有的功能代码供您参考:

import logging

import azure.functions as func

from azure.storage.blob import BlobClient


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    blob_client = BlobClient.from_blob_url("copy 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')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )