Intranetbee is a #No.1 SharePoint intranet portal

JOIN OVER 3,000

Subscribers who get fresh content to help their business weekly...

Categories
Categories

In this article we’ll discuss how to add function triggers to Azure Blob storage. It’s quite an interesting topic that needs to be discussed in both serverless end as well as the storage end. Let’s discuss how Azure function from the serverless end plays an important role in azure storage especially azure blob storage.
Before getting into the topic, it’s mandatory to know about azure functions and storage in separate.

What is Azure function?

Azure function is a part of azure compute solution. It is an event driven, Compute on demand experience that allows the existing azure application with code triggers.
In simple words, it’s nothing but an event trigger. It works as an event handler whenever the events raised by an event source.
There are many kinds of event sources like Azure blob storage, IoT, Cloud Events, Custom events and so on. In this article demo, we are going to see Azure Blob storage as the event source.
Similarly, like Azure functions there are many event handlers available like Azure Logic Apps, Event hubs, WebHooks and so on.

What is Azure Blob storage?

Azure blob storage is a part of azure storage solutions. It is used to store unstructured data in Azure. It can be a file format, audio or video format too. It is used to store data in On-Premises as well as the cloud hosted environments. It is also used to store data for backup and disaster recovery purposes.
In this article, Azure Blob storage acts as an Event source and Azure function acts as an event handler.
Whenever the blobs are created or deleted the event will be triggered using azure functions. In this article azure function will trigger an event when the blobs are created on the storage container.

Business case:

In real time scenario, client requests to create an application to process the photo that uploaded into the azure blob storage. It acts as a track to view the daily creation or deletion status of blobs in the storage account containers. At the end of the day, customers will get updates of blob from the function app.
Navigate to azure portal create an Azure storage account as given below in the screenshots.

Enter the required fields and click create. Once Deployment is done, Navigate to the storage account Blob storage Container.

Create a new container as given below in the screenshots. Here, demo container is created from the azure portal.

Open Visual Studio 2019 Create new project Search for Azure function Click Create.

Create an azure function application as given below in the screenshots. Choose the Blob storage, similarly you can choose HTTP triggers too. In this article blob storage is used as an event source, hence it is chosen.

The function app loads as given below in the screenshots. Navigate to app setting.json Change AzureWebJobStorage to the connection string value, which you can get from the azure portal storage account.

Now go back to Function1.cs file and mention the container name in the code in which you need to trigger an event.

Code:

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp4
{
public static class Function1
{
[FunctionName(“Function1”)]
public static void Run([BlobTrigger(“demo/{name}”, Connection = “AzureWebJobsStorage”)]Stream myBlob, string name, ILogger log)
{
log.LogInformation($”C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes”);
}
}
}

Once you added the container, either you can upload an image into the container using the Cloud explorer as given below in the screenshot or via Azure portal.

Navigate to View Cloud ExplorerChoose the storage account Choose the Blob container Upload the file.

Here png format file is uploaded into the demo container via cloud explorer in the Visual studio.

Now run the fun app, it will prompt the console window which shows the uploaded Image in the blob container.

Leave the application in a running state, Navigate to the azure portal. Go to the same container Try to upload an image from the portal end.

Now open the command prompt again, it will show up the recent file that is uploaded to the container from the portal end as shown below in the screenshot.
The code written above works for blob creation, similarly we can do the same procedure for blob deletion, update and so on.

In this article, we have seen how azure functions work with azure blob storage which you can utilize to automate or monitor and meet your customer needs.

Leave a Reply

Your email address will not be published. Required fields are marked *