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 and monitor Azure applications hosted on serverless architecture using Azure functions

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781800206601
Length 458 pages
Edition 3rd 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 (14) Chapters Close

Preface 1. Accelerating cloud app development using Azure Functions 2. Working with notifications using the SendGrid and Twilio services FREE CHAPTER 3. Seamless integration of Azure Functions with Azure Services 4. Developing Azure Functions using Visual Studio 5. Exploring testing tools for Azure functions 6. Troubleshooting and monitoring Azure Functions 7. Developing reliable serverless applications using durable functions 8. Bulk import of data using Azure Durable Functions and Cosmos DB 9. Configuring security for Azure Functions 10. Implementing best practices for Azure Functions 11. Configuring serverless applications in the production environment 12. Implementing and deploying continuous integration using Azure DevOps 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.

lock icon The rest of the chapter is locked
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