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 |
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.
- Create or use an existing function app
- Create or use an existing storage account
- Have version 5.0+ of the Microsoft.Azure.WebJobs.Extensions.Storage extension installed
- Download ngrok to allow Azure to call your local function
-
Open your function app in Visual Studio Code.
-
Press F1 to create a new blob trigger function. Make sure to use the connection string for your storage account.
-
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. -
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" } ] }
-
Set a breakpoint in your function on the line that handles logging.
-
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]
Once the Blob Trigger recognizes a new file is uploaded to the storage container, the break point is hit in your local function.
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>
To clean up the resources created in this article, delete the event grid subscription you created in this tutorial.