繁体   English   中英

事件网格触发器 Azure 具有 Blob 存储输入和 output 的函数 Python

[英]Event Grid Trigger Azure Functions with blob storage input and output Python

我正在寻找可以帮助我开发和部署事件网格触发器的适当资源/教程,该触发器将等待图像上传到 blob 容器,使用 python 处理该图像,然后将结果保存在另一个 blob 容器中。 我发现许多单独的文档在逻辑上并不一定会引导我使用 Azure 门户和 VSCode 进行开发和部署,就像从头到尾的逐步演练一样,所有步骤使这成为可能。

任何指导将不胜感激。

不建议你直接用Azure传送门来开发。 And since you need to use develop Azure function based on Python, then you need to use Azure function core tools.

1、下载并安装Azure function核心工具。(下载function3.x)

https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#v2

并且您需要安装 python 3.8.x(请注意您必须下载 64 位。否则 function 应用程序将在启动时遇到错误。)

2、配置Python到系统路径。

3、在VS代码中安装function扩展和Python调试扩展。

4、按f1并输入'func',你会发现创建function选项。

5,按照您将创建 function 的步骤。 然后使用下面的代码:

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)

完成上述所有步骤后,创建事件网格订阅并将 function 应用程序部署到 azure。 将事件网格指向您的 function 应用程序。 那么当A事件来临时,function就会被触发。

需要注意的是 Python 中不支持 Binder,所以......运行时无法指定路径。(很遗憾。Ibinder 仅支持基于 .Net 的语言。)

以上是你的要求,它可以工作,但blob路径不能是动态的......我认为对于你的情况,更合适的方法是使用blobtrigger和blob output绑定。(这可以从触发器获取blob名称到output。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM