-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Azure Serverless Computing Cookbook
By :

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.
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.
Perform the following steps:
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.
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:
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); }
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.userprofileimagecontainer
blob container. You should find a new blob:The image shown in Figure 1.17 can be viewed through any image viewing tool (such as MS Paint or Internet Explorer).
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.
The rand-guid
parameter will generate a new GUID and is assigned to the blob that gets created each time the trigger is fired.
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.