[英]Event Grid Trigger Azure Functions with blob storage input and output Python
I am looking for the appropriate resources/tutorials that could help me develop and deploy an Event Grid Trigger that would wait for an image upload to a blob container, process that image using python, then save the results in another blob container.我正在寻找可以帮助我开发和部署事件网格触发器的适当资源/教程,该触发器将等待图像上传到 blob 容器,使用 python 处理该图像,然后将结果保存在另一个 blob 容器中。 I have found many separate pieces of documentation which don't necessarily logically walk me through developing and deploying using the Azure Portal and VSCode, like a step-by-step walkthrough from beginning to end, of all the steps to make this possible.
我发现许多单独的文档在逻辑上并不一定会引导我使用 Azure 门户和 VSCode 进行开发和部署,就像从头到尾的逐步演练一样,所有步骤使这成为可能。
Any guidance would be greatly appreciated.任何指导将不胜感激。
I don't recommand you to use Azure portal directly to develop.不建议你直接用Azure传送门来开发。 And since you need to use develop Azure function based on Python, then you need to use Azure function core tools.
And since you need to use develop Azure function based on Python, then you need to use Azure function core tools.
1, Download and install Azure function core tools.(download the function3.x) 1、下载并安装Azure function核心工具。(下载function3.x)
https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#v2 https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#v2
And you need to install python 3.8.x(Please notice you must download 64bit. Otherwise function app will face error when start.)并且您需要安装 python 3.8.x(请注意您必须下载 64 位。否则 function 应用程序将在启动时遇到错误。)
2, Configure the Python to the system path. 2、配置Python到系统路径。
3, install function extension and Python debug extension in VS code. 3、在VS代码中安装function扩展和Python调试扩展。
4, press f1 and input 'func', you will find the create function options. 4、按f1并输入'func',你会发现创建function选项。
5, Follow the steps you will create the function. 5,按照您将创建 function 的步骤。 Then use below code:
然后使用下面的代码:
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "eventGridTrigger",
"name": "event",
"direction": "in"
},
{
"name": "inputblob",
"type": "blob",
"path": "test1/6.txt",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"name": "outputblob",
"type": "blob",
"path": "test2/6.txt",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
]
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyStorageConnectionAppSetting":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
}
}
__init__py
import json
import logging
import azure.functions as func
def main(event: func.EventGridEvent, inputblob: func.InputStream, outputblob: func.Out[func.InputStream]):
result = json.dumps({
'id': event.id,
'data': event.get_json(),
'topic': event.topic,
'subject': event.subject,
'event_type': event.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)
#Do something here. you can change below inputblob to other stream.
outputblob.set(inputblob)
After all of the above steps, create a event grid subscription and deploy your function app to azure.完成上述所有步骤后,创建事件网格订阅并将 function 应用程序部署到 azure。 Point the event grid to your function app.
将事件网格指向您的 function 应用程序。 Then When A event comes, the function will be triggered.
那么当A事件来临时,function就会被触发。
The thing you need notice is Binder not supported in Python, so...the path can not be specified in running.(This is a pity. Ibinder only supported for.Net based language.)需要注意的是 Python 中不支持 Binder,所以......运行时无法指定路径。(很遗憾。Ibinder 仅支持基于 .Net 的语言。)
Above is your requirement, It can works but the blob path cannot be dynamic... I think for your situation, the more suitable way is to use blobtrigger and blob output binding.(This can get the blob name from trigger to output.)以上是你的要求,它可以工作,但blob路径不能是动态的......我认为对于你的情况,更合适的方法是使用blobtrigger和blob output绑定。(这可以从触发器获取blob名称到output。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.