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

Jay*_*uks 7 azure-blob-storage azure-data-lake azure-devops azure-functions azure-function-app

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

谢谢

Hur*_*hen 10

对于此需求,您可以先转到您的存储 Blob,然后单击“生成 SAS ”来生成“ Blob SAS URL ”(您还可以定义 url 的开始日期和到期日期)。 在此输入图像描述

然后转到您的 python 函数,azure-storage-blob通过在 VS code 中运行命令来安装模块pip install azure-storage-blob。之后编写函数代码如下: 在此输入图像描述

test1.txt启动该函数并触发它,我们可以看到打印出来的内容logging.info在此输入图像描述

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

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
        )

Run Code Online (Sandbox Code Playgroud)