Book Image

Azure Serverless Computing Cookbook

By : Praveen Kumar Sreeram
Book Image

Azure Serverless Computing Cookbook

By: Praveen Kumar Sreeram

Overview of this book

Microsoft provides a solution to easily run small segment of code in the Cloud with Azure Functions. Azure Functions provides solutions for processing data, integrating systems, and building simple APIs and microservices. The book starts with intermediate-level recipes on serverless computing along with some use cases on benefits and key features of Azure Functions. Then, we’ll deep dive into 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. Moving on, you’ll get practical recipes on integrating DevOps with Azure functions, and providing continuous integration and continous deployment with Visual Studio Team Services. It also provides hands-on steps and tutorials based on real-world serverless use cases, to guide you through configuring and setting up your serverless environments with ease. Finally, you’ll see how to manage Azure functions, providing enterprise-level security and compliance to your serverless code architecture. By the end of this book, you will have all the skills required to work with serverless code architecture, providing continuous delivery to your users.
Table of Contents (11 chapters)

Saving the profile images to Queues using Queue output bindings

In the previous recipe, you have learnt how to receive two string parameters firstname and lastname in the Request body, and store them in the Azure Table storage. In this recipe, you will learn how to receive a URL of an image and save the same in the Blob container of an Azure Storage account.

We could have processed the downloaded user profile image in the recipe Persisting employee details using Azure Storage table output bindings. However, keeping in mind the size of the profile pictures, the processing of images on the fly in the HTTP requests might hinder the performance of the function. 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.

Getting ready

We will be updating the code of the RegisterUser function that we have used in the previous recipes.

How to do it…

  1. Navigate to the Integrate tab of the RegisterUser HTTP trigger function.
  2. Click on the New Output button and select Azure Queue Storage then click on the Select button.
  3. Provide the following parameters in the Azure Queue Storage output settings:
    • Queue name: Set the value of the Queue name as userprofileimagesqueue
    • Storage account connection: Please make sure that you select the right storage account in the Storage account connection field
    • Message parameter name: Set the name of the parameter to objUserProfileQueueItem which will be used in the Run method
  4. Click on Save to the create the new output binding.
  1. In this recipe, we will look at another approach of grabbing the request parameters for which we will use the Newtonsoft.JSON library to parse the JSON data. Let's navigate to the View files tab as shown in the following screenshot:
  1. As shown in the preceding screenshot, click on Add to add a new file. Please make sure that you name it as project.json as shown in the preceding screenshot.
  2. Once the file is created, add the following code to the project.json file. The following code adds the reference of the Newtonsoft.Json library.
        {
"frameworks" : {
"net46": {
"dependencies":{
"Newtonsoft.Json" : "10.0.2"
}
}
}
}
  1. Navigate back to the code editor by clicking on the function name (RegisterUser in this example) and paste the following code:
        #r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
public static void Run(HttpRequestMessage req,
TraceWriter log,
CloudTable
objUserProfileTable,
out string
objUserProfileQueueItem
)
{
var inputs = req.Content.ReadAsStringAsync().Result;
dynamic inputJson = JsonConvert.DeserializeObject<dynamic>
(inputs);

string firstname= inputJson.firstname;
string lastname=inputJson.lastname;
string profilePicUrl = inputJson.ProfilePicUrl;

objUserProfileQueueItem = profilePicUrl;
UserProfile objUserProfile = new UserProfile(firstname,
lastname, profilePicUrl);
TableOperation objTblOperationInsert =
TableOperation.Insert(objUserProfile);

objUserProfileTable.Execute(objTblOperationInsert);
}

public class UserProfile : TableEntity
{
public UserProfile(string lastname, string firstname,
string profilePicUrl)
{
this.PartitionKey = "p1";
this.RowKey = Guid.NewGuid().ToString();
this.FirstName = firstname;
this.LastName = lastname;
this.ProfilePicUrl = profilePicUrl;
}
public UserProfile() { }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ProfilePicUrl {get; set;}
}
  1. Click on Save to save the code changes in the code editor of the run.csx file.
  1. Let's test the code by adding another parameter ProfilePicUrl to the Request body shown as follows then click on the Run button in the Test tab of the Azure Function code editor window: The image used in the below JSON might not exist when you are reading this book. So, Please 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 fine you will see the Status : 200 OK message, then 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 have provided in the Step 3. Following is the screenshot of the Queue message that was created:

How it works…

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

  • Added a reference to the Newtonsoft.Json NuGet library in the project.json file
  • Added a new parameter named out string objUserProfileQueueItem which is used to bind the URL of the profile picture as a Queue message content
  • We have also made the Run method synchronous by removing async as it doesn't allow us to have out parameters

There's more…

The project.json file contains all the references of the external libraries that we may use in the Azure Function.

At the time of writing, Azure Function Runtime only supports .NET Framework 4.6.

See also

  • The Persisting employee details using Azure Storage table Output Bindings recipe