Azure(函数)参数是在Python中声明的,但不是在function.json中声明的
我不明白发生了什么事。 我严格遵循所有的微软文档,事实上甚至不使用我自己的任何脚本/代码。 首先,我按照他们的文档创建Python函数。成功了。 第二个文档使用命令行工具将Azure功能连接到Azure存储。不可复制。 我严格地遵循每一步,但却收到了错误 更令人惊讶的是,他们最终向我展示了与第一篇文章不同的代码。我试了两种版本,都没用 这些是他们文档中的代码。 这是他们的python脚本代码(init.py) 这是JSON函数代码:Azure(函数)参数是在Python中声明的,但不是在function.json中声明的,python,azure,azure-functions,Python,Azure,Azure Functions,我不明白发生了什么事。 我严格遵循所有的微软文档,事实上甚至不使用我自己的任何脚本/代码。 首先,我按照他们的文档创建Python函数。成功了。 第二个文档使用命令行工具将Azure功能连接到Azure存储。不可复制。 我严格地遵循每一步,但却收到了错误 更令人惊讶的是,他们最终向我展示了与第一篇文章不同的代码。我试了两种版本,都没用 这些是他们文档中的代码。 这是他们的python脚本代码(init.py) 这是JSON函数代码: { "scriptFile": &
{
"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"
}
]
}
因为他们没有发布完整版本的代码,我添加了括号之类的东西。如果你想引用文档,他们会这么说:
尽管一个函数只能有一个触发器,但它可以有多个输入和输出绑定,这允许您连接到其他Azure服务和资源,而无需编写自定义集成代码。
您可以在function文件夹的function.json文件中声明这些绑定。在上一个quickstart中,HttpExample文件夹中的function.json文件在bindings集合中包含两个绑定:
每个绑定至少有一个类型、一个方向和一个名称。在上面的示例中,第一个绑定是httpTrigger类型,方向为。对于in方向,name指定由触发器调用时发送到函数的输入参数的名称
集合中的第二个绑定是http类型,其方向为out,在这种情况下,$return的特殊名称表示此绑定使用函数的返回值,而不是提供输入参数
要从此函数写入Azure存储队列,请添加一个名为msg的queue类型的out绑定,如下代码所示:
"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作为输出参数提供给函数。对于队列类型,还必须在queueName中指定队列的名称,并在connection中提供Azure存储连接的名称(来自local.settings.json)
因此,即使这段代码是在文档中发布的,它似乎也不能正常工作。或者他们的python代码不起作用。我不知道
我试着跟着他们的脚步走。
我还尝试复制粘贴他们的新版本代码(与原始版本略有不同)
但什么都不管用
我仍然会遇到这个错误(我更改了一些我怀疑敏感的元素)
我真的无法理解他们自己的代码是如何不起作用的。我想做的就是学习如何将我的python脚本部署到Azure,我没想到这会是一个如此大的挑战
这就是我更新的pythoninit.py代码在示例回答后的样子:
下面是示例回答后更新的function.json代码的样子:
这就是location.setting的外观(敏感信息已更改)
这就是host.js的样子
试试下面的方法,效果会很好:
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
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.")
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureStorageQueuesConnectionString":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
\uuuu init\uuuuu.py
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
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.")
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureStorageQueuesConnectionString":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
function.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
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.")
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureStorageQueuesConnectionString":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
local.settings.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
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.")
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureStorageQueuesConnectionString":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
你能具体说明一下吗。AzureWebJobsStorage和AzureStorageQueuesConnectionString是否具有相同的值?或者我应该离开“AzureWebJobsStorage”:“像这样吗?”@twmp我可以向你解释这件事。AzureWebJobsStorage是azure函数的内置值。许多函数操作都需要基于此存储(无论是实际存储还是基于SQL的存储模拟器),并且在部署到Azure时,它需要存储函数的基本信息。@twmp AzureStorageQueuesConnectionString是我随意使用的一个键,您可以为它命名任何名称。简而言之,一个是在设计时出现的,另一个是定制的。您可以将两者指定为一件事,例如,使用真实的存储连接字符串作为两者的值在本地进行测试(local.settings.json仅在本地工作,所以可以随意操作。)。注释不用于扩展讨论;这段对话已经结束。