Storing images in Azure Blob Storage
The previous recipe explained how to store an image URL in a queue message. Let's learn how to trigger an Azure function (queue trigger) when a new queue item is added to the Azure Queue storage service. Each message in the queue is a URL of the profile picture of a user, which will be processed by Azure Functions and stored as a blob in the Azure Blob Storage service.
Getting ready
While the previous recipe focused on creating queue output bindings, this recipe will explain how to grab an image's URL from a queue, create a corresponding byte array, and then write it to a blob.
Note that this recipe is a continuation of the previous recipes. Be sure to implement them first.
How to do it…
Perform the following steps:
- Create a new Azure function by choosing Azure Queue Storage Trigger from the templates.
- Provide the following details after choosing the template:
Name the function: Provide a meaningful name, such as CreateProfilePictures.
Queue name: Name the queue userprofileimagesqueue. This will be monitored by the Azure function. Our previous recipe created a new item for each of the valid requests coming to the HTTP trigger (named RegisterUser) into the userprofileimagesqueue queue. For each new entry of a queue message to this queue storage, the CreateProfilePictures trigger will be executed automatically.
Storage account connection: Connection of the storage account based on where the queues are located.
- Review all the details and click on Create to create the new function.
- Navigate to the Integrate tab, click on New Output, choose Azure Blob Storage, and then click on the Select button.
- In the Azure Blob Storage output section, provide the following:
Blob parameter name: Set this to outputBlob.
Path: Set this to userprofileimagecontainer/{rand-guid}.
Storage account connection: Choose the storage account for saving the blobs and click on the Save button:
Figure 1.16: Azure Blob storage output binding settings
- Click on the Save button to save all the changes.
- Replace the default code of the run.csx file of the CreateProfilePictures function with the following code. The code grabs the URL from the queue, creates a byte array, and then writes it to a blob:
using System;
public static void Run(Stream outputBlob, string myQueueItem, ILogger log) {
byte[] imageData = null;
using(var wc = new System.Net.WebClient())
{
imageData = wc.DownloadData(myQueueItem);
}
outputBlob.WriteAsync(imageData, 0, imageData.Length);
}
- Click on the Save button to save the changes. Make sure that there are no compilation errors in the Logs window.
- Let's go back to the RegisterUser function and test it by providing the firstname, lastname, and ProfilePicUrl fields, as we did in the Saving profile picture paths to queues using queue output bindings recipe.
- Navigate to the Azure Storage Explorer window and look at the userprofileimagecontainer blob container. You should find a new blob:
Figure 1.17: Azure Storage Explorer
The image shown in Figure 1.17 can be viewed through any image viewing tool (such as MS Paint or Internet Explorer).
How it works…
We have created a queue trigger that gets executed when a new message arrives in the queue. Once it finds a new queue message, it reads the message, which is the URL of a profile picture. The function makes a web client request, downloads the image data in the form of a byte array, and then writes the data into the output blob.
There's more…
The rand-guid parameter will generate a new GUID and is assigned to the blob that gets created each time the trigger is fired.
Note
It is mandatory to specify the blob container name in the Path parameter of the Blob storage output binding while configuring the Blob storage output. Azure Functions creates the container automatically if it doesn't exist.
Queue messages can only be used to store messages up to 64 KB in size. To store messages greater than 64 KB, developers must use Azure Service Bus.
In this recipe, you learned how to invoke an Azure function when a new queue item is added to the Azure Storage Queue service. In the next recipe, you'll learn how to resize an image.