Skip to content

Files

Latest commit

ac31058 · Dec 29, 2021

History

History
164 lines (116 loc) · 5.09 KB

functions-event-grid-blob-trigger.md

File metadata and controls

164 lines (116 loc) · 5.09 KB
title description author ms.topic ms.date ms.author ms.devlang
Azure Functions Event Grid Blob Trigger
Learn to setup and debug with the Event Grid Blob Trigger
cachai2
conceptual
3/1/2021
cachai
csharp, java, python

Azure Function Event Grid Blob Trigger

This article demonstrates how to debug and deploy a local Event Grid Blob triggered function that handles events raised by a storage account.

Note

The Event Grid Blob trigger is in preview.

Prerequisites

Create a new function

  1. Open your function app in Visual Studio Code.

  2. Press F1 to create a new blob trigger function. Make sure to use the connection string for your storage account.

  3. The default url for your event grid blob trigger is:

    http://localhost:7071/runtime/webhooks/blobs?functionName={functionname}
    http://localhost:7071/runtime/webhooks/blobs?functionName=Host.Functions.{functionname}
    http://localhost:7071/runtime/webhooks/blobs?functionName=Host.Functions.{functionname}

    Note your function app's name and that the trigger type is a blob trigger, which is indicated by blobs in the url. This will be needed when setting up endpoints later in the how to guide.

  4. Once the function is created, add the Event Grid source parameter.

    Add Source = BlobTriggerSource.EventGrid to the function parameters.

    [FunctionName("BlobTriggerCSharp")]
    public static void Run([BlobTrigger("samples-workitems/{name}", Source = BlobTriggerSource.EventGrid, Connection = "connection")]Stream myBlob, string name, ILogger log)
    {
        log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    }

    Add "source": "EventGrid" to the function.json binding data.

    {
      "scriptFile": "__init__.py",
      "bindings": [
        {
          "name": "myblob",
          "type": "blobTrigger",
          "direction": "in",
          "path": "samples-workitems/{name}",
          "source": "EventGrid",
          "connection": "MyStorageAccountConnectionString"
        }
      ]
    }

    Press F5 to build the function. Once the build is complete, add "source": "EventGrid" to the function.json binding data.

    {
      "scriptFile" : "../java-1.0-SNAPSHOT.jar",
      "entryPoint" : "com.function.{MyFunctionName}.run",
      "bindings" : [ {
        "type" : "blobTrigger",
        "direction" : "in",
        "name" : "content",
        "path" : "samples-workitems/{name}",
        "dataType" : "binary",
        "source": "EventGrid",
        "connection" : "MyStorageAccountConnectionString"
       } ]
    }

  5. Set a breakpoint in your function on the line that handles logging.

  6. Start a debugging session.

    Press F5 to start a debugging session.

    Press F5 to start a debugging session.

    Open a new terminal and run the below mvn command to start the debugging session.

    mvn azure-functions:run

[!INCLUDE functions-event-grid-local-dev]

Debug the function

Once the Blob Trigger recognizes a new file is uploaded to the storage container, the break point is hit in your local function.

Deployment

As you deploy the function app to Azure, update the webhook endpoint from your local endpoint to your deployed app endpoint. To update an endpoint, follow the steps in Add a storage event and use the below for the webhook URL in step 5. The <BLOB-EXTENSION-KEY> can be found in the App Keys section from the left menu of your Function App.

https://<FUNCTION-APP-NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=<FUNCTION-NAME>&code=<BLOB-EXTENSION-KEY>
https://<FUNCTION-APP-NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=Host.Functions.<FUNCTION-NAME>&code=<BLOB-EXTENSION-KEY>
https://<FUNCTION-APP-NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=Host.Functions.<FUNCTION-NAME>&code=<BLOB-EXTENSION-KEY>

Clean up resources

To clean up the resources created in this article, delete the event grid subscription you created in this tutorial.

Next steps