【问题标题】:Event Grid Trigger Azure Functions with blob storage input and output Python事件网格使用 blob 存储输入和输出 Python 触发 Azure Functions
【发布时间】:2021-03-08 20:44:32
【问题描述】:

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

任何指导将不胜感激。

【问题讨论】:

  • 嗨,有更新吗?

标签: python azure-functions azure-eventgrid


【解决方案1】:

我不建议您直接使用 Azure 门户进行开发。而既然需要使用基于Python开发Azure函数,那么就需要使用Azure函数核心工具。

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

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

并且你需要安装python 3.8.x(请注意你必须下载64位,否则函数应用程序启动时会遇到错误。)

2、配置Python到系统路径。

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

4、按f1并输入'func',你会找到创建函数选项。

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

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)

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

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

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

【讨论】:

  • 快速指导:如果您可以在提交之前对您的作品运行英语拼写检查器,我们将不胜感激。任何英语变体都很好。如今,大多数浏览器都支持拼写检查,而且通常已经安装了一个。
猜你喜欢
  • 2018-04-19
  • 2021-10-21
  • 2021-11-02
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2017-04-06
相关资源
最近更新 更多