0

我在使用 python 在 Azure 中为 blobtrigger 案例定义多个输出时遇到问题。如何将 WhileLoop 的所有尝试设置为 Outputblob?现在,我只得到最后一个实体(替换以前的实体)。我把代码用于--init--.py:

import logging
import json
import azure.functions as func
from urllib.request import urlopen

#from azure.storage import blob

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")
    
    blob=  inputblob.read()
    blob_invoices= json.loads(blob)
    Number_of_Invoices=int(len(blob_invoices))
    j=0
    while j < Number_of_Invoices:
        Invoice_url= blob_invoices[j]["invoiceFile"]['url']
        invoice_opening= urlopen(Invoice_url)
              
        invoice_content= invoice_opening.read()
        outputblob.set(invoice_content)

if __name__ == "__main__":
    main()

和function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "data4test1/{name}.json",
      "connection": "data4test_STORAGE"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "direction": "out",
      "path": "data4test2/{name}.xml",
      "connection": "data4test_STORAGE"
    }

  ],
  "disabled": false
4

1 回答 1

0

下面的初始化示例代码将帮助您使用 python 从 Azure 函数 blob 触发器设置多个输出绑定:

import logging
import azure.functions as func


def main(inputblob: func.InputStream,
        outputblob1: func.Out[func.InputStream],
        outputblob2: func.Out[func.InputStream],
        outputblob3: 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")
    outputblob1.set(inputblob)
    outputblob2.set(inputblob)
    outputblob3.set(inputblob)

函数.json 文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputblob",
      "type": "blobTrigger",
      "direction": "in",
      "dataType": "binary",
      "path": "blobcontainer/{name}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputblob1",
      "type": "blob",
      "path": "uploadblobcontainer/{blobtrigger}-first",
      "dataType": "binary",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    },
    {
      "name": "outputblob2",
      "type": "blob",
      "path": "uploadblobcontainer/{blobtrigger}-second",
      "dataType": "binary",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    },
    {
      "name": "outputblob3",
      "type": "blob",
      "path": "uploadblobcontainer/{blobtrigger}-third",
      "dataType": "binary",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ]
}

更新的答案

要列出容器中的 blob, 请使用list_blobs方法。此方法返回一个生成器。以下代码将容器中每个 blob的名称输出到控制台。

generator = block_blob_service.list_blobs('mycontainer')
for blob in generator:
    print(blob.name)

同样,在 for 循环中定义所需的功能。

于 2021-10-28T04:28:51.833 回答