Python中的Blob触发Azure函数

时间:2020-09-04 14:29:56

标签: python-3.x azure-functions azure-storage-blobs azure-blob-storage

我正在尝试在Python中创建Blob触发器Azure函数,该函数会自动将特定Excel文件中的所有工作表拆分为单独的.csv文件,并将其分散到同一Azure Blob容器中。我的 init.py function.json 文件看起来像这样: function.json 文件:

        {
        "scriptFile": "__init__.py",
        "bindings": [
            {
                "name": "blobin",
                "type": "blobTrigger",
                "direction": "in",
                "path": "folder/ExcelFile.xlsx",
                "connection": "blobSTORAGE"
            },
            {
                "name": "blobout",
                "type": "blob",
                "direction": "out",
                "path": "folder/{name}",
                "connection": "blobSTORAGE"
            }
        ]
    }

init .py 文件:

import logging
from xlrd import open_workbook
import csv
import azure.functions as func

def main(blobin: func.InputStream, blobout: func.Out[bytes]):
    logging.info(f"Python blob trigger function processed blob \n")
    try:
        wb = open_workbook('ExcelFile.xlsx')
        for i in range(0, wb.nsheets):
            sheet = wb.sheet_by_index(i)
            print(sheet.name)
            with open("Sheet_%s.csv" %(sheet.name.replace(" ","")), "w") as file:
                writer = csv.writer(file, delimiter = ",")
                print(sheet, sheet.name, sheet.ncols, sheet.nrows)
                header = [cell.value for cell in sheet.row(0)]
                writer.writerow(header)
                for row_idx in range(1, sheet.nrows):
                    row = [int(cell.value) if isinstance(cell.value, float) else cell.value
                           for cell in sheet.row(row_idx)]
                    writer.writerow(row)

            blobout.set(file.read)
            logging.info(f"Split sheet %s into CSV successfully.\n" %(sheet.name))
    except:
        print("Error")

我尝试在PC上运行纯Python代码,但未实现Azure函数,但成功了。但是,当我将该功能部署到Azure Apps时,当我尝试上载Excel文件时,它不会“触发”。我认为我输入的配置错误,但不知道如何确认或修复它。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

由于您在本地没有问题,所以我认为问题来自环境变量中未设置的blobSTORAGE。

在本地,环境变量在local.settings.json的values部分中设置。但是,当您部署功能应用程序时,它将从该位置获取环境变量:

enter image description here