Book Image

Azure Serverless Computing Cookbook - Second Edition

By : Praveen Kumar Sreeram, Marston
Book Image

Azure Serverless Computing Cookbook - Second Edition

By: Praveen Kumar Sreeram, Marston

Overview of this book

Microsoft provides a solution for easily running small segments of code in the cloud with Azure Functions. The second edition of Azure Serverless Computing Cookbook starts with intermediate-level recipes on serverless computing along with some use cases demonstrating the benefits and key features of Azure Functions. You’ll explore the core aspects of Azure Functions, such as the services it provides, how you can develop and write Azure Functions, and how to monitor and troubleshoot them. As you make your way through the chapters, you’ll get practical recipes on integrating DevOps with Azure Functions, and providing continuous integration and continuous deployment with Azure DevOps. This book also provides hands-on, step-by-step tutorials based on real-world serverless use cases to guide you through configuring and setting up your serverless environments with ease. You will also learn how to build solutions for complex, real-world, workflow-based scenarios quickly and with minimal code using Durable Functions. In the concluding chapters, you will ensure enterprise-level security within your serverless environment. The most common tips and tricks that you need to be aware of when working with Azure Functions on production environments will also be covered in this book. By the end of this book, you will have all the skills required for working with serverless code architecture, providing continuous delivery to your users.
Table of Contents (13 chapters)

Saving the profile images to Queues using Queue output bindings

In the previous recipe, you learned how to receive two string parameters, firstname and lastname, in the Request body, and stored them in Azure Table storage. In this recipe, we'll add a new parameter named ProfilePicUrl for the profile picture of the user that is publicly accessible via the internet. In this recipe, you will learn how to receive the URL of an image and save the URL in the Blob container of an Azure Storage account.

You might be thinking that the ProfilePicUrl input parameter could have been used to download the picture from the internet in the previous recipe, Persisting employee details using Azure Storage table output bindings. We didn't do this because the size of the profile pictures might be huge, considering the modern technology that's available today, and so processing images on the fly in the HTTP requests might hinder the performance of the overall application. For that reason, we will just grab the URL of the profile picture and store it in Queue, and later we can process the image and store it in the Blob container.

Getting ready

We will be updating the code of the RegisterUser function that we 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 value of the Queue name to userprofileimagesqueue
    • Storage account connection: Make sure that you 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 marked bold that are given in the following code:
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);
}
  1. In the preceding code, we 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 the ProfilePicUrl into the Queue. Now, Click on Save to save the code changes in the code editor of the run.csx file.
  2. Let's test the code by adding another parameter, ProfilePicUrl, in the Request body and then click on the Run button in the Test tab of the Azure Function code editor window. The image that's used in the following JSON might not exist when you are reading this book. So, make sure that you provide a valid URL of the image:
{
"firstname": "Bill",
"lastname": "Gates",
"ProfilePicUrl":"https://upload.wikimedia.org/wikipedia/ commons/1/19/Bill_Gates_June_2015.jpg"
}
  1. If everything goes well you will see a Status : 200 OK message. The image URL that you have passed as an input parameter in the Request body will be created as a Queue message in the Azure Storage Queue service. Let's navigate to Azure Storage Explorer and view the Queue named userprofileimagesqueue, which is the Queue name that we provided in step 3. The following is a screenshot of the Queue message that was created:

How it works...

In this recipe, we added Queue message output binding and made the following changes to the code:

  • Added a new parameter named out string objUserProfileQueueItem, which is used to bind the URL of the profile picture as a Queue message content
  • Used the AddAsync method of IAsyncCollector to use the Run method, which saves the profile URL to the Queue as a Queue message