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
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Azure Serverless Computing Cookbook

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

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789615265
Length 424 pages
Edition 2nd Edition
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Jason Marston Jason Marston
Author Profile Icon Jason Marston
Jason Marston
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 (13) Chapters Close

Preface 1. Developing Cloud Applications Using Function Triggers and Bindings FREE CHAPTER 2. Working with Notifications Using the SendGrid and Twilio Services 3. Seamless Integration of Azure Functions with Azure Services 4. Understanding the Integrated Developer Experience of Visual Studio Tools 5. Exploring Testing Tools for the Validation of Azure Functions 6. Monitoring and Troubleshooting Azure Serverless Services 7. Developing Reliable Serverless Applications Using Durable Functions 8. Bulk Import of Data Using Azure Durable Functions and Cosmos DB 9. Implementing Best Practices for Azure Functions 10. Configuring of Serverless Applications in the Production Environment 11. Implementing and Deploying Continuous Integration Using Azure DevOps 12. Other Books You May Enjoy

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
You have been reading a chapter from
Azure Serverless Computing Cookbook - Second Edition
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781789615265
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