![](/img/trans.png)
[英]Azure Function & TimerTrigger in a Custom Container: what else do I need in the function.json file?
[英]Azure (functions) Parameters are declared in Python but not in function.json
我無法理解發生了什么。 我從字面上遵循所有 Microsoft 文檔,實際上甚至不使用我自己的任何腳本/代碼。 首先,我按照他們的文檔創建了 Python function。 有效。 https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=azure-cli%2Ccmd%2Cbrowser Second docs to connect Azure Functions to Azure Storage using command線工具。 不可重現。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-add-output-binding-storage-queue-cli?pivots=programming-language-python&tabs=bash%2Cbrowser我真的按照每一步,但收到錯誤。
更令人驚訝的是,他們最終向我展示了與第一篇文章不同的代碼。 我嘗試了兩個版本——沒有一個工作。
這些是他們文檔中的代碼。 這是他們的 python 腳本代碼( init .py)
import logging
import azure.functions as func
def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
msg.set(name)
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
這是 JSON function 代碼:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
}
由於他們沒有發布完整版本的代碼,我添加了諸如括號之類的東西。 如果您想要文檔引用,那就是他們所說的:
雖然 function 只能有一個觸發器,但它可以有多個輸入和 output 綁定,讓您無需編寫自定義集成代碼即可連接到其他 Azure 服務和資源。 您在 function 文件夾中的 function.json 文件中聲明這些綁定。 在上一個快速入門中,HttpExample 文件夾中的 function.json 文件在 bindings 集合中包含兩個綁定:
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
每個綁定至少有一個類型、一個方向和一個名稱。 在上面的示例中,第一個綁定的類型為 httpTrigger,方向為 in。對於 in 方向,name 指定輸入參數的名稱,該輸入參數在觸發器調用時發送到 function。
集合中的第二個綁定類型為 http,方向為 out,在這種情況下,$return 的特殊名稱表明此綁定使用函數的返回值,而不是提供輸入參數。
要從此 function 寫入 Azure 存儲隊列,請添加名為 msg 的隊列類型的輸出綁定,如下面的代碼所示:
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
在這種情況下,將 msg 作為 output 參數提供給 function。 對於隊列類型,您還必須在 queueName 中指定隊列的名稱,並在連接中提供 Azure 存儲連接的名稱(來自 local.settings.json)。
因此,即使此代碼已發布在文檔中,它似乎也無法正常工作。 或者他們的 python 代碼不起作用。 我不知道。
我試着按照他們的步驟去做。 我還嘗試復制粘貼他們的新代碼版本(與原始代碼略有不同)但沒有任何效果我仍然收到此錯誤(我更改了一些我懷疑是敏感的元素)
(.venv) C:\Users\usr\LocalFunctionProj>func start
Found Python version 3.8.5 (py).
Azure Functions Core Tools
Core Tools Version: 3.0.3160 Commit hash: 00aa7f49cc5c5f15241a5e6e5363256f19ceb980
Function Runtime Version: 3.0.14916.0
Functions:
HttpExample: [GET,POST] http://localhost:8072/api/HttpExample
For detailed output, run func with --verbose flag.
[2020-12-27T08:45:11.912Z] Worker process started and initialized.
[2020-12-27T08:45:12.048Z] Worker failed to function id ebece17c-3077-4f78-bcca-d46565cef86c.
[2020-12-27T08:45:12.050Z] Result: Failure
Exception: FunctionLoadError: cannot load the HttpExample function: the following parameters are declared in Python but not in function.json: {'msg'}
Stack: File "D:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 272, in _handle__function_load_request
self._functions.add_function(
File "D:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8\WINDOWS\X64\azure_functions_worker\functions.py", line 112, in add_function
raise FunctionLoadError(
.
[2020-12-27T08:45:16.457Z] Host lock lease acquired by instance ID '000000000000000000000000AF616381'.
我真的無法理解他們自己的代碼如何不起作用。 我只想學習如何將我的 python 腳本部署到 Azure,沒想到會是這么大的挑戰。
這就是我更新的 python init .py 代碼在示例答案后的樣子:
這是示例答案后更新的 function.json 代碼的樣子:
試試下面,它會正常工作:
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
__init__.py
import logging
import azure.functions as func
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
msg.set("This is test. 1227")
return func.HttpResponse("This is a test.")
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureStorageQueuesConnectionString"
}
]
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureStorageQueuesConnectionString":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.