简体   繁体   English

Azure Blob 触发器 Python Function 写入 CosmosDB 错误

[英]Azure Blob Trigger Python Function to Write To CosmosDB Error

I have JSON documents being uploaded into an Azure Blob Container and I have written an Azure Python Function to write the JSON into CosmosDB. I have JSON documents being uploaded into an Azure Blob Container and I have written an Azure Python Function to write the JSON into CosmosDB. The triggering works fine, but I get an error.触发工作正常,但我得到一个错误。 Here is the Python function:这是 Python function:

import logging
import azure.functions as func

def main(myblob: func.InputStream, doc: func.Out[func.Document]):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    
    json_data = myblob.read()
    try:
        # Store output data using Cosmos DB output binding
        doc.set(func.Document.from_json(json_data))
    except Exception as e:
        logging.info(f"Error: {e}")
        print('Error:')
        print(e)

Here is the function.json file:这是 function.json 文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "cloud-save-blob-container/{name}",
      "connection": "cloudsavestorage_STORAGE"
    },
    {
      "type": "cosmosDB",
      "name": "userJson",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "createIfNotExists": false,
      "connectionStringSetting": "MyCosmosDBConnectionString",
      "direction": "out"
    }
  ],
  "disabled": false
}

This the error I see in the Azure Portal:这是我在 Azure 门户中看到的错误:

Result: Failure Exception: FunctionLoadError: cannot load the JsonBlobTrigger1 function: the following parameters are declared in Python but not in function.json: {'doc'} Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 290, in _handle__function_load_request self._functions.add_function( File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/functions.py", line 112, in add_function raise FunctionLoadError( Result: Failure Exception: FunctionLoadError: cannot load the JsonBlobTrigger1 function: the following parameters are declared in Python but not in function.json: {'doc'} Stack: File "/azure-functions-host/workers/python/3.8/LINUX /X64/azure_functions_worker/dispatcher.py”,第 290 行,在 _handle__function_load_request self._functions.add_function(文件“/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/functions.py”,第 112 行,在 add_function 中引发 FunctionLoadError(

Thanks for assistance.感谢您的帮助。

I have fixed the problem and my Python Function gets triggered and puts data into Cosmos DB just like I want.我已经解决了这个问题,我的 Python Function 被触发并将数据放入 Cosmos DB,就像我想要的那样。 The problem was with my Python code.问题出在我的 Python 代码上。 I had "doc" in two places where it should have been "userJson".我在两个应该是“userJson”的地方有“doc”。 One, in the "def main(...):" line and again in the "try:" block, "doc.set(...)", which didn't match the Output binding name in function.json.一,在“def main(...):”行中,再次在“try:”块中,“doc.set(...)”,它与function.Z466DEEC76ECDF5FCA65D3875中的Output绑定名称不匹配。 Once I changed the two occurrences of "doc" to "userJson" it worked.一旦我将两次出现的“doc”更改为“userJson”,它就起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM