我正在尝试使用 python v2 编程模型在 azure 函数应用程序中编写一个非常基本的函数。我想要它做的就是连接到 CosmosDB 帐户并返回名为“systems”的给定容器中列出的所有文档。将来它将返回一组经过过滤的文档,但目前其中只有一个文档,因此不需要过滤。
这是目前基于改编这些来自azure的示例的功能:
import azure.functions as func
import logging
import json
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
@app.function_name(name="system")
@app.route(route="system")
@app.cosmos_db_input(arg_name="documents",
database_name="database",
container_name="systems",
connection_string_setting="DATABASE_CONNECTION_STRING")
def system(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse(
json.dumps(documents),
status_code=200
)
当我尝试使用 Azure Function Core Tools 运行该函数时 (核心工具版本:4.0.5198 提交哈希:N/A(64 位) 函数运行时版本:4.21.1.20667
我收到以下错误,我一生都无法弄清楚我需要为“连接”参数提供哪些信息。基于所有 azure 演示,我只需要提供 local.settings.json 文件中可用的名为 DATABASE_CONNECTION_STRING 的连接字符串。
[2023-10-02T10:11:56.430Z] Worker failed to index functions
[2023-10-02T10:11:56.432Z] Result: Failure
Exception: TypeError: BindingApi.cosmos_db_input() missing 1 required positional argument: 'connection'
Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 338, in _handle__functions_metadata_request
fx_metadata_results = self.index_functions(function_path)
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 607, in index_functions
indexed_functions = loader.index_function_app(function_path)
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call
return func(*args, **kwargs)
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\loader.py", line 151, in index_function_app
imported_module = importlib.import_module(module_name)
File "C:\Users\gcookman\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\gcookman\Code\KPMG-DAI-API\function_app.py", line 9, in <module>
@app.cosmos_db_input(arg_name="documents",
任何帮助或指导表示赞赏:)
您有多种配置。
connection_string_setting
适用于旧扩展,而 container_name
适用于新/当前扩展。
根据错误,您似乎正在使用新的。
将
connection_string_setting
更改为 connection
。函数文档似乎有点偏离(https://learn.microsoft.com/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv4&pivots =programming-language-python#configuration)因为他们没有 _
,但名称是正确的。