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:
Step 1: Create an Azure Function APP
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" /> </ItemGroup> </Project>
|
#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; }
}
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:
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.