简体   繁体   中英

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. 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.

Any guidance would be greatly appreciated.

I don't recommand you to use Azure portal directly to develop. 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)

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.)

2, Configure the Python to the system path.

3, install function extension and Python debug extension in VS code.

4, press f1 and input 'func', you will find the create function options.

5, Follow the steps you will create the 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. Point the event grid to your function app. Then When A event comes, the function will be triggered.

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.)

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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