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

Read data from Azure blob storage in Azure Function in python

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

谢谢

对于这个需求,你可以先去你的存储blob然后点击“Generate SAS”来生成“Blob SAS URL”(您还可以定义 url 的开始日期和到期日期)。

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

启动函数并触发,可以看到logging.info打印出test1.txt的内容。

下面是我的全部功能代码供大家参考:

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
        )