使用 python 在本地(使用 Azure 存储模拟器和 Azure 存储资源管理器)与 Azure 函数进行输出绑定时不创建文件

问题描述 投票:0回答:1

我有一个 blob 容器和两个文件夹。(输入,输出)当一个 excel 文件放在输入文件夹中时,我想触发该函数,编译代码后我想将输出 excel 文件放在名为的文件夹中'输出'。我正在使用python

我使用输入绑定来触发该功能。编译代码后,我完成了输出绑定,将新创建的文件放入输出文件夹。但是,它没有发生。

我正在使用 Azure 存储模拟器和 Azure 存储资源管理器在本地执行此操作。

这是我的function.json文件。

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "cloudops-resources/inputs/{name}",
      "connection": "AzureWebJobsStorage"
    },
    {
    "name": "outputblob",
    "type": "blob",
    "direction": "out",
    "path": "cloudops-resources/outputs/{name}",
    "connection": "AzureWebJobsStorage"
    }
  ],
  "disabled": false
}

这是我的local.settings.json文件。

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobs.my_function.Disabled": "true"
  }
}

我尝试使用此代码。

这是我的init.py


def main(inputblob: func.InputStream, outputblob: func.Out[func.InputStream]):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {inputblob.name}\n"
                 f"Blob Size: {inputblob.length} bytes")

.
.#some code here
.
.

#Reading the input xlsx file from customer
    df0 = pd.read_excel(inputblob)

    #Opening a new workbook and a worksheet
    wb = Workbook()
    sheet = wb.active

.
.#some code here
.

  #Saving the file
    today = date.today()
    date_today = today.strftime("%m-%d-%y")
    outputblob = "{}_COMPLETE_{}.xlsx".format(date_today,inputblob)
    wb.save(outputblob)
    return func.Out[func.InputStream](
         body=outputblob,
         status_code=200
    )

我也提到了这些。

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=asgi%2Capplication-level&pivots=python-mode-configuration#outputs:~:text=account% 20guidance.-,Outputs,-Output%20can%20be

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-output?tabs=in-process&pivots=programming-language-python#:~:text=Here %27s%20the%20Python%20code%3A

有人可以帮我解决这个问题吗?

python azure-functions azure-blob-storage local function-binding
1个回答
0
投票

我按照下面的步骤得到了想要的结果:-

我的init.py代码:-

import  logging

  

import  openpyxl

  

import  azure.functions  as  func

  
  

def  main(myblob: func.InputStream, outputBlob: func.Out[func.InputStream], context: func.Context):

logging.info(f"Python blob trigger function processed blob \n"

f"Name: {myblob.name}\n"

f"Blob Size: {myblob.length} bytes")

# wb = openpyxl.load_workbook(myblob)

# sheet = wb.active

  

outputBlob.set(myblob)

logging.info(f"Excel file has been copied to the output container.")

我没有使用任何openpyxl代码,而是直接上传outputBlob中的文件。

我的function.json文件:-

{

"scriptFile": "__init__.py",

"bindings": [

{

"name": "myblob",

"type": "blobTrigger",

"direction": "in",

"path": "strg65/{name}",

"connection": "sidstrg54_STORAGE"

},

{

"type": "blob",

"direction": "out",

"name": "outputBlob",

"path": "strg66/{name}",

"connection": "sidstrg54_STORAGE"

}

]

}

我在我的 function.json 中使用 {name} 作为输入和输出 blob 存储绑定,因此具有 excel 扩展名的文件被复制为相同的名称而不会在输出容器中损坏。

我的 local.settings.json 包含 azure blob 存储连接字符串:-

{

"IsEncrypted": false,

"Values": {

"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<storage-account>;AccountKey=<account-key>;EndpointSuffix=core.windows.net",

"FUNCTIONS_WORKER_RUNTIME": "python",

"sidstrg54_STORAGE": "DefaultEndpointsProtocol=https;AccountName=<storage-account>;AccountKey=<account-key>;EndpointSuffix=core.windows.net"

}

}

输出:-

enter image description here

文件被复制到 outputBlob 容器,如下所示:-

enter image description here

下载复制的blob,内容可见:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.