无法在门户中运行Azure Blob触发的函数

huangapple go评论66阅读模式
英文:

Not able to run Azure blob triggered function in portal

问题

我需要使Blob触发器在Azure门户中正常工作,而不仅仅在本地使用VS Code中有效。有人可以告诉我我漏掉了什么吗?

英文:

I'm new in developing with Azure. Currently I'm trying to develop a blob triggered python function which was initially developed using VS Code. When it was tested by running in VS Code, it worked perfectly and got the expected output, however when it was deployed to an Azure function app, the function doesn't trigger even though I'm uploading sample blob files to the specified container. When checking the function's monitor, it says 'no results'.
I tried creating a new function directly in the portal (not deploying via VS Code) and this time it triggers perfectly.

Below is the function.json and init.py of my function, it reads an XML file uploaded in container 'invoices', reads and creates a pandas df based on an excel table stored in container 'files' and checks if the invoice's supplier exists in the excel table, and finally prints a dictionary that contains the invoice ID and the name of the supplier. It does some secondary things like printing the result of the invoice reading process (done with beautifulsoup) just to verify it's working perfectly.
function.json:

{
  "bindings": [
    {
      "name": "myblob",
      "path": "invoices",
      "connection": "my storage account's name",
      "direction": "in",
      "type": "blobTrigger"
    }
  ]
}

init.py:

import logging
import azure.functions as func
from openpyxl import load_workbook
import openpyxl
import io
from azure.storage.blob import BlobServiceClient
import pandas as pd
from bs4 import BeautifulSoup

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    conn_str1 = "DefaultEndpointsProtocol=https;AccountName=**********;AccountKey=*********;EndpointSuffix=core.windows.net"
    container1 = "invoices"
    blob_name = myblob.name[9:]
    blob_service_client1 = BlobServiceClient.from_connection_string(conn_str1)
    container_client1 = blob_service_client1.get_container_client(container1)
    download_blob1 = container_client1.download_blob(blob_name)
    soup = BeautifulSoup(download_blob1, features='xml')


    #Extracción de razon social, ruc y fecha de emision del xml
    rsocial = soup.find("cbc:RegistrationName").string.extract()
    print("Razón social = ", rsocial)
    ruc = soup.find("cac:AccountingSupplierParty").find("cbc:ID").string.extract()
    print("RUC = ", ruc)
    fecha = soup.find("cbc:IssueDate")
    print(" Fecha de emisión = ", fecha.string.extract())


    #Carga de tabla de Excel con el RUC de los proveedores de energía:
    conn_str2 = "DefaultEndpointsProtocol=https;AccountName=**********;AccountKey=*********;EndpointSuffix=core.windows.net"
    container2 = "files"
    xl_blob = "lista_ruc_2.xlsx"
    blob_service_client2 = BlobServiceClient.from_connection_string(conn_str2)
    container_client2 = blob_service_client2.get_container_client(container2)
    download_blob2 = container_client2.download_blob(xl_blob)
    file = io.BytesIO(download_blob2.readall())
    wb = openpyxl.load_workbook(file)  
    ws = wb["Tabelle1"]
    mapping = {}
    # Extracción del contenido de la tabla Excel y conversión a un pandas df
    for entry, data_boundary in ws.tables.items():
        data = ws[data_boundary]

        content = [[cell.value for cell in ent] 
                for ent in data
            ]
        
        header = content[0]
        rest = content[1:]
        
        df = pd.DataFrame(rest, columns = header)
        mapping[entry] = df

    #¿Es factura de energía? + Obtener n° de factura + Registro en el Excel de factuas correspondiente
    if int(ruc) in df['RUC'].unique():
        print('Es factura de energía')
        #Si es Luz del Sur:
        if int(ruc) == 20331898008:
            print('Es Luz del Sur')
            list_n_factura = soup.find_all("cbc:ID")
            n = 0
            while n <= len(list_n_factura):
                if n == 5:
                    n_factura = list_n_factura[n].string.extract()
                    print("N° factura = ", n_factura)
                n=n+1
        #Si es Enel:
        elif int(ruc) == 20269985900:
            print('Es Enel')
            list_n_factura = soup.find_all("cbc:ID")
            n = 0
            while n <= len(list_n_factura):
                if n == 1:
                    n_factura = list_n_factura[n].string.extract()
                    print("N° factura = ", n_factura)
                n=n+1
        #Si es Red de Energía del Perú:
        elif int(ruc) == 20504645046:
            print('Es Red de Energía del Perú')
            list_n_factura = soup.find_all("cbc:ID")
            n = 0
            while n <= len(list_n_factura):
                if n == 1:
                    n_factura = list_n_factura[n].string.extract()
                    print("N° factura = ", n_factura)
                n=n+1
        #Si es Agua Azul:
        elif int(ruc) == 20538865312:
            print('Es Agua Azul')
            list_n_factura = soup.find_all("cbc:ID")
            n = 0
            while n <= len(list_n_factura):
                if n == 0:
                    n_factura = list_n_factura[n].string.extract()
                    print("N° factura = ", n_factura)
                n=n+1
    else: 
        print('No es factura de energía')
        exit()

    #Post request con el diccionario conteniendo razon social y n° factura
    data = {'Razón Social': rsocial, 'N° Factura': n_factura}
    print(data)

    print('\n------ FIN DEL PROGRAMA ------\n')

As explained above, what I need is to get the blob trigger working in the azure portal too, not just in local by VS Code. Can someone please tell me what I'm missing?

答案1

得分: 0

"for anyone facing the same issue, I could solve it just by adding a new application setting in which you have to include the connection string of the blob trigger's container. I thought it was automatically added as I deployed the function, but it seems it's necessary to add it manually."

英文:

for anyone facing the same issue, I could solve it just by adding a new application setting in which you have to include the connection string of the blob trigger's container. I thought it was automatically added as I deployed the function, but it seems it's necessary to add it manually.

huangapple
  • 本文由 发表于 2023年3月31日 04:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定