且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-14 09:02:14

对于此要求,您可以先转到存储 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
        )