首页 > 解决方案 > 在天蓝色如何根据 blob 存储文件内容进行 api 调用?

问题描述

我想先说我是 Azure 的初学者。

我希望能够将一个 excel 文件上传到我的 blob 存储中,其中每一行代表要传递给 api 调用的参数。然后,我想获取每个 api 调用的结果,并将它们放在 excel 文件中每一行的最后一列中。

我尝试使用数据工厂,但找不到根据文件内容更改 api 调用的方法,也无法写入 excel 文件。因此,我决定尝试 Azure 逻辑应用程序,它会根据上传到 blob 存储的文件触发,然后触发 azure 函数。

我遇到的问题是,我不知道如何将 excel 文件中的行内容传递给 azure 函数,以便创建 API 调用。我也不完全确定设置应该是什么样子,我以前没有使用过 Azure 功能。

如果使用 azure 逻辑应用程序 + 函数并不理想,我也愿意接受完全不同的方法。

标签: azureazure-blob-storage

解决方案


如果您使用的是 Python Azure 函数,只需尝试以下代码:

import logging
from os import remove
import azure.functions as func
import tempfile
import pandas as pd
import requests


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob uri: {myblob.uri} bytes")

    temp_file_path = tempfile.gettempdir() + "\\temp.xlsx"
    #download .xlsx as a temp file 
    with open(temp_file_path,"w+b") as temp_blob:
        temp_blob.write(myblob.read())

    #read .xlsx and send request 
    dfs = pd.read_excel(temp_file_path,engine='openpyxl')
    for reqInfo in dfs.values:
        logging.info('request method:'+ reqInfo[0])
        logging.info('request URL:'+ reqInfo[1])
        response = requests.request(reqInfo[0],reqInfo[1])
        logging.info(response.status_code)

    #remove temp file after use
    remove(temp_file_path)

要求.txt:

azure-functions
pandas 
openpyxl
requests

这是我的演示 .xlsx 文件内容:

在此处输入图像描述

在我将文件上传到我在函数触发器中指定的容器后,结果如下:

在此处输入图像描述

因此,基本上,您只能使用 Azure 功能来满足您的要求。

有关 Azure 函数 blob 触发器的更多信息,请参阅此处


推荐阅读