Push Blob Storage events to Azure Storage Table using Azure Function App

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

 

Scenario:

In this blog, we will cover the steps to trigger an Azure Function App based on any of the Azure Blob Storage events and log the events to the Azure table storage.

Below is the step by step approach to accomplish this requirement:

Pre-Requisites:

  • Azure Storage GPV2 account
  • Function App
  • Event Grid

Step 1: Create an Azure Function APP

  • To create function app we will leverage this article.
  • Once the function app is created, you should see all its resource dependencies like below:

clipboard_image_0.png

  • Click on the Azure Function app which you just created and then you will be able to see the blade as shown below. Click on Plus (+) Sign to add Function, select Event Grid trigger and function name:

clipboard_image_1.png

  • On your local computer, create a file named “function.proj” and paste the below content in it:

 

<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>

              <TargetFramework>netstandard2.0</TargetFramework>

   </PropertyGroup>

   <ItemGroup>

               <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />

   </ItemGroup>

</Project>

 

 

clipboard_image_2.png

  • Double click on “csx” file to edit the content and paste the below code snippet to push the event to Azure Table Storage.

#r "Microsoft.Azure.EventGrid"

using Microsoft.Azure.EventGrid.Models;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Table;

using System.Threading.Tasks;

 

public static void Run(EventGridEvent eventGridEvent, ILogger log)

    {

         CloudStorageAccount storageAccount = new CloudStorageAccount(

         new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("MyStorage", "yvQCeS8/ok************************** "), true);

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

CloudTable test = tableClient.GetTableReference("EventsData");

 

TableData data = new TableData(eventGridEvent.Data.ToString());

data.data = eventGridEvent.Data.ToString();

 

// Create the TableOperation that inserts the customer entity.

TableOperation insertOperation = TableOperation.Insert(data);

 

// Execute the insert operation.

test.ExecuteAsync(insertOperation);

log.LogInformation(eventGridEvent.Data.ToString());

}

 

public class TableData : TableEntity

{

   public TableData(string data)

   {

       this.PartitionKey = Guid.NewGuid().ToString();

       this.RowKey = Guid.NewGuid().ToString();

   }

 

   public TableData() { }

 

   public string data { get; set; }  

}

  • You can test if the newly created Function app is running without any issues, as per the below screenshot:

      

clipboard_image_3.png

 

  • Once the function app is created and tested successfully. You need to click on the Integrate setting in the function app blade to integrate with the Event Grid. You need to copy the Event Grid subscription URL as this will be used as a webhook subscriber endpoint in Azure Event Grid.

              

clipboard_image_4.png

 

Enable Blob events

The steps to create storage account and configure event grid is discusssed here.

Follow the below steps to configure the event grid to react to the storage events:

  1. Once you have created a GPv2 storage account, go to your storage account and click the Events settings in the middle blade and click on the Event Subscription button:

 

                

clipboard_image_5.png

  1. Here you need to add the webhook endpoint which you had copied in Step 8 mentioned above.

            

clipboard_image_6.png

You can test the functionality using storage explorer by uploading a blob to your storage account. You should be able to see the blob events in the Azure Table storage.

Attached:

  • proj
  • csx

REMEMBER: these articles are REPUBLISHED. Your best bet to get a reply is to follow the link at the top of the post to the ORIGINAL post! BUT you're more than welcome to start discussions here:

This site uses Akismet to reduce spam. Learn how your comment data is processed.