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 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

Storing images in Azure Blob Storage

The previous recipe explained how to store an image URL in a queue message. Let's learn how to trigger an Azure function (queue trigger) when a new queue item is added to the Azure Queue storage service. Each message in the queue is a URL of the profile picture of a user, which will be processed by Azure Functions and stored as a blob in the Azure Blob Storage service.

Getting ready

While the previous recipe focused on creating queue output bindings, this recipe will explain how to grab an image's URL from a queue, create a corresponding byte array, and then write it to a blob.

Note that this recipe is a continuation of the previous recipes. Be sure to implement them first.

How to do it…

Perform the following steps:

  1. Create a new Azure function by choosing Azure Queue Storage Trigger from the templates.
  2. Provide the following details after choosing the template:

    Name the function: Provide a meaningful name, such as CreateProfilePictures.

    Queue name: Name the queue userprofileimagesqueue. This will be monitored by the Azure function. Our previous recipe created a new item for each of the valid requests coming to the HTTP trigger (named RegisterUser) into the userprofileimagesqueue queue. For each new entry of a queue message to this queue storage, the CreateProfilePictures trigger will be executed automatically.

    Storage account connection: Connection of the storage account based on where the queues are located.

  3. Review all the details and click on Create to create the new function.
  4. Navigate to the Integrate tab, click on New Output, choose Azure Blob Storage, and then click on the Select button.
  5. In the Azure Blob Storage output section, provide the following:

    Blob parameter name: Set this to outputBlob.

    Path: Set this to userprofileimagecontainer/{rand-guid}.

    Storage account connection: Choose the storage account for saving the blobs and click on the Save button:

    Azure Blob storage output binding settings
    Figure 1.16: Azure Blob storage output binding settings
  6. Click on the Save button to save all the changes.
  7. Replace the default code of the run.csx file of the CreateProfilePictures function with the following code. The code grabs the URL from the queue, creates a byte array, and then writes it to a blob:
    using System;
    public static void Run(Stream outputBlob, string myQueueItem, ILogger log) {
      byte[] imageData = null;
      using(var wc = new System.Net.WebClient()) 
      {
        imageData = wc.DownloadData(myQueueItem);
      }
      outputBlob.WriteAsync(imageData, 0, imageData.Length);
    }
  8. Click on the Save button to save the changes. Make sure that there are no compilation errors in the Logs window.
  9. Let's go back to the RegisterUser function and test it by providing the firstname, lastname, and ProfilePicUrl fields, as we did in the Saving profile picture paths to queues using queue output bindings recipe.
  10. Navigate to the Azure Storage Explorer window and look at the userprofileimagecontainer blob container. You should find a new blob:
    Viewing the output in Azure Storage Explorer
Figure 1.17: Azure Storage Explorer

The image shown in Figure 1.17 can be viewed through any image viewing tool (such as MS Paint or Internet Explorer).

How it works…

We have created a queue trigger that gets executed when a new message arrives in the queue. Once it finds a new queue message, it reads the message, which is the URL of a profile picture. The function makes a web client request, downloads the image data in the form of a byte array, and then writes the data into the output blob.

There's more…

The rand-guid parameter will generate a new GUID and is assigned to the blob that gets created each time the trigger is fired.

Note

It is mandatory to specify the blob container name in the Path parameter of the Blob storage output binding while configuring the Blob storage output. Azure Functions creates the container automatically if it doesn't exist.

Queue messages can only be used to store messages up to 64 KB in size. To store messages greater than 64 KB, developers must use Azure Service Bus.

In this recipe, you learned how to invoke an Azure function when a new queue item is added to the Azure Storage Queue service. In the next recipe, you'll learn how to resize an image.

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