Azure HTTPtrigger 函数不写入 Azure 存储队列
Azure HTTPtrigger function not writing to Azure Storage Queue
我希望下面的代码从 func.HttpRequest
获取 JSON 正文,将该消息写入 Azure 存储队列,然后 return 向调用方发送一条成功消息。这有效,除了我的存储队列是空白的。
import logging
import azure.functions as func
def main(req: func.HttpRequest,
orders: func.Out[func.QueueMessage]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
message = req.get_json()
logging.info(message)
orders.set(message)
return func.HttpResponse(
body=”success”,
status_code=200
)
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": "orders",
"queueName": "preprocess",
"connection": "orders_STORAGE"
}
]
}
Local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_ER_RUNTIME": "python",
"AzureWebJobsStorage": "AzureWebJobsStorage",
"orders_STORAGE": "DefaultEndpointsProtocol=https;AccountName=orders;AccountKey=*****;EndpointSuffix=core.windows.net"
}
}
终端输出:
…
[4/17/2019 5:54:39 PM] 正在执行 'Functions.QueueTrigger'(原因='在 'preprocess' 上检测到新队列消息',Id=f27fd7d1-1ace-****-*** *-00fb021c9ca4)
[4/17/2019 5:54:39 PM] 触发器详细信息:MessageId:d28f96c5-****-****-9191-93f96a4423de,DequeueCount:1,InsertionTime:4/17/2019 5:54:35下午+00:00
[4/17/2019 5:54:39 PM] 信息:收到 FunctionInvocationRequest,请求 ID:5bf59a45-****-****-9705-173d9635ca94,函数 ID:fa626dc9-*** *-****-a59b-6a48f08d87e1,调用 ID:f27fd7d1-1ace-****-****-00fb021c9ca4
[4/17/2019 5:54:39 PM] Python 队列触发器函数处理了一个队列项:name2
[4/17/2019 5:54:39 PM] 信息:已成功处理 FunctionInvocationRequest,请求 ID:5bf59a45-****-****-9705-173d9635ca94,函数 ID:fa626dc9-3313- ****-****6a48f08d87e1,调用 ID:f27fd7d1-1ace-****-****-00fb021c9ca4
[4/17/2019 5:54:39 PM]执行'Functions.QueueTrigger'(成功,Id=f27fd7d1-1ace-****-****-00fb021c9ca4)
INFO: Successfully processed
– 让我觉得这行得通,我应该在我的队列中看到一条消息,但它是空白的。
为什么我在队列中看不到消息?
谢谢
您的终端输出显示 QueueTrigger 检测到新消息 preprocess
,因此实际上它已被写入。
至于你的队列中没有消息,因为它被处理到你的函数。消息发送后,将从队列中删除。这就是为什么你的队列是空白的。
从教程中:Test the function,您还可以找到描述:
Back in Storage Explorer, click Refresh and verify that the message
has been processed and is no longer in the queue.
我希望下面的代码从 func.HttpRequest
获取 JSON 正文,将该消息写入 Azure 存储队列,然后 return 向调用方发送一条成功消息。这有效,除了我的存储队列是空白的。
import logging
import azure.functions as func
def main(req: func.HttpRequest,
orders: func.Out[func.QueueMessage]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
message = req.get_json()
logging.info(message)
orders.set(message)
return func.HttpResponse(
body=”success”,
status_code=200
)
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": "orders",
"queueName": "preprocess",
"connection": "orders_STORAGE"
}
]
}
Local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_ER_RUNTIME": "python",
"AzureWebJobsStorage": "AzureWebJobsStorage",
"orders_STORAGE": "DefaultEndpointsProtocol=https;AccountName=orders;AccountKey=*****;EndpointSuffix=core.windows.net"
}
}
终端输出:
… [4/17/2019 5:54:39 PM] 正在执行 'Functions.QueueTrigger'(原因='在 'preprocess' 上检测到新队列消息',Id=f27fd7d1-1ace-****-*** *-00fb021c9ca4)
[4/17/2019 5:54:39 PM] 触发器详细信息:MessageId:d28f96c5-****-****-9191-93f96a4423de,DequeueCount:1,InsertionTime:4/17/2019 5:54:35下午+00:00
[4/17/2019 5:54:39 PM] 信息:收到 FunctionInvocationRequest,请求 ID:5bf59a45-****-****-9705-173d9635ca94,函数 ID:fa626dc9-*** *-****-a59b-6a48f08d87e1,调用 ID:f27fd7d1-1ace-****-****-00fb021c9ca4
[4/17/2019 5:54:39 PM] Python 队列触发器函数处理了一个队列项:name2
[4/17/2019 5:54:39 PM] 信息:已成功处理 FunctionInvocationRequest,请求 ID:5bf59a45-****-****-9705-173d9635ca94,函数 ID:fa626dc9-3313- ****-****6a48f08d87e1,调用 ID:f27fd7d1-1ace-****-****-00fb021c9ca4
[4/17/2019 5:54:39 PM]执行'Functions.QueueTrigger'(成功,Id=f27fd7d1-1ace-****-****-00fb021c9ca4)
INFO: Successfully processed
– 让我觉得这行得通,我应该在我的队列中看到一条消息,但它是空白的。
为什么我在队列中看不到消息?
谢谢
您的终端输出显示 QueueTrigger 检测到新消息 preprocess
,因此实际上它已被写入。
至于你的队列中没有消息,因为它被处理到你的函数。消息发送后,将从队列中删除。这就是为什么你的队列是空白的。
从教程中:Test the function,您还可以找到描述:
Back in Storage Explorer, click Refresh and verify that the message has been processed and is no longer in the queue.