在 python 中从 Azure Function 中的 Azure blob 存储读取数据 [英] Read data from Azure blob storage in Azure Function in python

查看:27
本文介绍了在 python 中从 Azure Function 中的 Azure blob 存储读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please how do I read in data from my Azure Storage account when I launch my Function app. I need to read the saved weights for my machine learning model at runtime. I want to read the model directly from the storage account because the model is expected to be updated daily and do not want have to manually redeploy the model.

谢谢

推荐答案

对于此要求,您可以先转到存储 blob,然后单击生成 SAS";生成Blob SAS URL";(您还可以定义 url 的开始日期和到期日期).

For this requirement, you can go to your storage blob first and click "Generate SAS" to generate "Blob SAS URL" (you can also define the start date and expiry date of the url).

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

Then go to your python function, install azure-storage-blob module by running pip install azure-storage-blob command in VS code. After that, write the function code like:

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

Start the function and trigger it, we can see the content of test1.txt printed out by logging.info.

以下是我所有的功能代码供大家参考:

Below is all of my function code for your reference:

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
        )

这篇关于在 python 中从 Azure Function 中的 Azure blob 存储读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆