Search icon CANCEL
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 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

Storing the image in Azure Blob storage

Let's learn how to invoke an Azure Function when a new queue item is added to the Azure Storage Queue service. Each message in the Queue is the URL of the profile picture of a user which will be processed by the Azure Functions and will be stored as a Blob in the Azure Storage Blob service.

Getting ready

In the previous recipe, we have learnt how to create Queue output bindings. In this recipe, you will grab the URL from the Queue, create a byte array, and then write it to a Blob.

This recipe is a continuation of the previous recipes. Please make sure that you implement them.

How to do it...

  1. Create a new Azure Function by choosing the QueueTrigger-C# from the templates.
  2. Provide the following details after choosing the template:
    • Name your function: Please provide a meaningful name such as CreateProfilePictures.
    • Queue name: Name of the Queue which should 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 where the Queues are located.
  3. Review all the details, and click on Create to create the new function.
  4. Navigate to Integrate tab then click on New Output then choose Azure Blob Storage then click on the Select button.
  5. In the Azure Blob Storage output section, provide the following:
    • Blob parameter name: Set it to outputBlob
    • Path: Set it to userprofileimagecontainer/{rand-guid}
    • Storage account connection: Choose the storage account where you would like to save the Blobs:
  1. Once you provide all the preceding details, click on the Save button to save all the changes.
  2. Replace the default code of the run.csx file with the following code:
        using System;
public static void Run(Stream outputBlob,string myQueueItem,
TraceWriter log)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
{
imageData = wc.DownloadData(myQueueItem);
}
outputBlob.WriteAsync(imageData,0,imageData.Length);
}
  1. Click on the Save button to save the changes.
  2. Let's go back to the RegisterUser function and test it by providing firstname, lastname, and ProfilePicUrl fields as we did in the Saving the profile images to Queues using Queue output bindings recipe.
  3. Now, navigate to the Azure Storage Explorer, and look at the Blob container userprofileimagecontainer. You will find a new Blob as shown in the following screenshot:
  1. You can view the image in any tool (such as MS Paint or Internet Explorer).

How it works...

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

There's more...

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

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 one automatically if it doesn't exist.

You can use Queue messages only when you would like to store messages which are up to 64 KB. If you would like to store the messages greater than 64 KB, you need to use the Azure Service Bus.

See also...

  • The Building a backend Web API using HTTP triggers recipe
  • The Persisting employee details using Azure Storage table output bindings recipe
  • The Saving the profile images to Queues using Queue output bindings recipe
  • The Storing the image in Azure Blob storage 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