Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Azure Serverless Computing Cookbook

You're reading from   Azure Serverless Computing Cookbook Build applications hosted on serverless architecture using Azure Functions

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781788390828
Length 332 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Praveen Kumar Sreeram Praveen Kumar Sreeram
Author Profile Icon Praveen Kumar Sreeram
Praveen Kumar Sreeram
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Accelerate Your Cloud Application Development Using Azure Function Triggers and Bindings FREE CHAPTER 2. Working with Notifications Using SendGrid and Twilio Services 3. Seamless Integration of Azure Functions with Other Azure Services 4. Understanding the Integrated Developer Experience of Visual Studio Tools for Azure Functions 5. Exploring Testing Tools for the Validation of Azure Functions 6. Monitoring and Troubleshooting Azure Serverless Services 7. Code Reusability and Refactoring the Code in Azure Functions 8. Developing Reliable and Durable Serverless Applications Using Durable Functions 9. Implement Best Practices for Azure Functions 10. Implement Continuous Integration and Deployment of Azure Functions Using Visual Studio Team Services

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
You have been reading a chapter from
Azure Serverless Computing Cookbook
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781788390828
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image