Book Image

Azure Serverless Computing Cookbook - Third Edition

By : Praveen Kumar Sreeram
Book Image

Azure Serverless Computing Cookbook - Third Edition

By: Praveen Kumar Sreeram

Overview of this book

This third edition of Azure Serverless Computing Cookbook guides you through the development of a basic back-end web API that performs simple operations, helping you understand how to persist data in Azure Storage services. You'll cover the integration of Azure Functions with other cloud services, such as notifications (SendGrid and Twilio), Cognitive Services (computer vision), and Logic Apps, to build simple workflow-based applications. With the help of this book, you'll be able to leverage Visual Studio tools to develop, build, test, and deploy Azure functions quickly. It also covers a variety of tools and methods for testing the functionality of Azure functions locally in the developer's workstation and in the cloud environment. Once you're familiar with the core features, you'll explore advanced concepts such as durable functions, starting with a "hello world" example, and learn about the scalable bulk upload use case, which uses durable function patterns, function chaining, and fan-out/fan-in. By the end of this Azure book, you'll have gained the knowledge and practical experience needed to be able to create and deploy Azure applications on serverless architectures efficiently.
Table of Contents (14 chapters)
13
Index

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:

  1. Create a new Azure function by choosing Azure Queue Storage Trigger from the templates.
  2. 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.

  3. Review all the details and click on Create to create the new function.
  4. Navigate to the Integrate tab, click on New Output, choose Azure Blob Storage, and then click on the Select button.
  5. 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:

    Azure Blob storage output binding settings
    Figure 1.16: Azure Blob storage output binding settings
  6. Click on the Save button to save all the changes.
  7. 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);
    }
  8. Click on the Save button to save the changes. Make sure that there are no compilation errors in the Logs window.
  9. 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.
  10. Navigate to the Azure Storage Explorer window and look at the userprofileimagecontainer blob container. You should find a new blob:
    Viewing the output in Azure Storage Explorer
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.