Integrate Azure Blob In Azure Functions

In this article, you will learn how to perform Azure blob binding in Azure functions.

Introduction

 
This article is in continuation of my previous article where I have explained how to perform Azure Queue Storage Binding for Azure Functions. Here, we are going to discuss how to integrate the Azure blob to Azure Functions. In my last article, we ended up by listening to the deserialized JSON Payload in Azure Queue Storage which is integrated with Azure Functions. Now, we are going to generate the invoice from a message in Azure Queue and store it in Azure blob.
 

Prerequisites

  • Azure Subscription 
  • Basic knowledge of Azure Functions and Storage Service.

Integrate Azure Blob to Azure Functions

   
In my last article, we created a function called GenerateInvoice which listened to Azure Queue Service integrated with QueueIntegrationDemo function as an output. Now, we are going to integrate the Blob Storage Service to the GenerateInvoice function as an output to store the data in a blob as a file. Here, I’m going to generate the invoice by listening to the queue and store it in the blob.
 
Log into the Azure portal, go to the Functions App under our existing GenerateInvoice function, and click "Integrate".  
 
Integrate Azure Blob in Azure Functions
Figure 1 

In the integration window, we need to add a new output,i.e., Azure Blob Storage. 

Integrate Azure Blob in Azure Functions
Figure 2
 
Give the blob path in Azure Blob Storage output form, select the storage account which is used for the Azure Queue Storage service (I have given the container name as invoicedetails and the invoice file name as invoice-{rand-guid}.txt). The {rand-guid} will generate a random GUID.  
 
Integrate Azure Blob in Azure Functions
Figure 3
  
If the container is not available in our storage account, the service will automatically create a container in the blob and add the file.
 
Let's go to the editor and update the function to generate the invoice file and store it in the blob.
  1. using System;  
  2. public class Order{  
  3.     public string OrderId{get;set;}  
  4.     public string ProductId{get;set;}  
  5.     public string UserEmail{get;set;}  
  6.     public decimal Price {get;set;}  
  7. }  
  8.   
  9. public static void Run(Order myQueueItem, ILogger log, TextWriter outputBlob)  
  10. {  
  11.     log.LogInformation($"Received an Order: {myQueueItem.OrderId}");  
  12.     outputBlob.WriteLine($"OrderId: {myQueueItem.OrderId}");  
  13.     outputBlob.WriteLine($"Email: {myQueueItem.UserEmail}");  
  14.     outputBlob.WriteLine($"OrderDate: {DateTime.UtcNow}");  
  15.     outputBlob.WriteLine($"Price: {myQueueItem.Price}");  
  16. }  
We have used TextWriter to write the order details in the text file which is created in the blob. Now, let’s trigger our function through POSTMAN.
 
 Integrate Azure Blob in Azure Functions
Figure 4 

I’m going to use Microsoft Azure Storage Explorer to check the invoice file.

 Integrate Azure Blob in Azure Functions
Figure 5 
 
Let's download the file and check the data.
 
Integrate Azure Blob in Azure Functions
Figure 6 
    
Open the function.json file in GenerateInvoice.
 
 Integrate Azure Blob in Azure Functions
Figure 7 

From the above code, you can see the bindings. The input is the Queue Trigger and the output of the function is a blob.

Conclusion


We have seen how to perform Azure Blob Storage Binding in the Azure Functions to generate the invoice file and store it in a blob. In my future article, we will see how to send an email of the order details using SendGrid Service with Azure Functions.