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

Saving profile picture paths to queues using queue output bindings

The previous recipe highlighted how to receive two string parameters, firstname and lastname, in the Request body and store them in Azure Table storage. In this recipe, let's add a new parameter named ProfilePicUrl for the profile picture of the user that is publicly accessible via the internet. In this recipe (and the next), you'll learn about the process of extracting the URL of an image and saving it in the blob container of an Azure storage account.

While the ProfilePicUrl input parameter can be used to download the picture from the internet, in the previous recipe, Persisting employee details using Azure Table storage output bindings, this was not feasible due to the time required to process the large size of the image, which might hinder the performance of the overall application. For this reason, it is faster to grab the URL of the profile picture and store it in a queue, which can be processed later before storing it in the blob.

Getting ready

We'll be updating the code of the RegisterUser function that was used in the previous recipes.

How to do it…

Perform the following steps:

  1. Navigate to the Integrate tab of the RegisterUser HTTP trigger function.
  2. Click on the New Output button, select Azure Queue Storage, and then click on the Select button.
  3. Provide the following parameters in the Azure Queue Storage output settings:

    Message parameter name: Set the name of the parameter to objUserProfileQueueItem, which will be used in the Run method.

    Queue name: Set the queue name to userprofileimagesqueue.

    Storage account connection: It is important to select the right storage account in the Storage account connection field.

  4. Click on Save to create the new output binding.
  5. Navigate back to the code editor by clicking on the function name (RegisterUser in this example) or the run.csx file and make the changes shown in the following code:
    public static async Task<IActionResult> Run( HttpRequest req,
    CloudTable objUserProfileTable, IAsyncCollector<string> public static async Task<IActionResult> Run( 
        HttpRequest req,
        CloudTable objUserProfileTable,
        IAsyncCollector<string> objUserProfileQueueItem,
        ILogger log)
        {....
        string firstname= inputJson.firstname;
        string profilePicUrl = inputJson.ProfilePicUrl;
        await objUserProfileQueueItem.AddAsync(profilePicUrl);
        ....
        objUserProfileTable.Execute(objTblOperationInsert);
        }
  6. In the preceding code, you have added queue output bindings by adding the IAsyncCollecter parameter to the Run method and just passing the required message to the AddAsync method. The output bindings will take care of saving ProfilePicUrl to the queue. Now, click on Save to save the code changes in the code editor of the run.csx file.
  7. Let's test the code by adding another parameter, ProfilePicUrl, to the Request body and then clicking on the Run button in the Test tab of the Azure Functions code editor window. Replace "URL here" with the URL of an image that's accessible over the internet; you'll need to make sure that the image URL provided is valid:
    {
    "firstname": "Bill",
    "lastname": "Gates", 
    "ProfilePicUrl":"URL here"
    }
  8. If everything goes fine, you'll see the Status: 200 OK message again. Then, the image URL that was passed as an input parameter in to the Request body will be created as a queue message in the Azure Queue storage service. Let's navigate to Azure Storage Explorer and view the queue named userprofileimagesqueue, which is the queue name that was provided in step 3.
  9. Figure 1.15 represents the queue message that was created:
    Viewing the output in Storage Explorer
Figure 1.15: Viewing the output in Storage Explorer

How it works…

In this recipe, we added a queue message output binding and made the following changes to our existing code:

  • We added a new parameter named out string objUserProfileQueueItem, which binds the URL of the profile picture as queue message content.
  • We used the AddAsync method of IAsyncCollector in the Run method that saves the profile URL to the queue as a queue message.

In this recipe, you learned how to receive a URL of an image and save it in the blob container of an Azure storage account. In the next recipe, we'll store an image in Azure Blob Storage.