Azure Blob, Azure Cloud, Azure Function, Azure Storage

How to use Azure Functions and trigger on new Item addition in Azure Storage using BlobTrigger 2/2: Part 11

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

                  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

We will continue the process on which we had paused in previous post. We were getting error when we tried to run the function, because we had not configured path, let us do that. Go to Integrate tab under function name, now configure the path as highlighted.

We have configured the path as “images/{name}.png”, this function will only trigger on blobs that are in images container and have .png file extension, of course you can modify it to fulfill your particular need.

Save the changes and come back to coding panel.

Now to test run we need to upload a blob to the images container, either from Azure Portal itself or by using the storage explorer. Click here to download it.

We are going to use Storage explorer, login with you subscription account and go to the storage account | container ( Images ) | upload a jpg image first and then check the logs in Azure Portal, you will see nothing happen, this is because we set the trigger for .png file extensions. Now upload a png file and see the logs, you will see that the function ran successfully.

Now as you can see that our integration is working fine and if you check the logs, there is a message printed.

[info] C# Blob trigger function processed blob

Name:debug

It is time to place our logic in this function.

Now before we put our code in function, we need to define the output so that we can save the resultant image file to the container. To set it up, Click on Integrate | click on New Output | click on Azure Blob Storage | select

Now change the Path to appropriate output location on which our resultant image will be stored, we are going to change it to images/{name}.jpg | click on Save. As blob will be stream of bytes at if you do not mention the file extension in the path then, it will save without file extension and when you access it, it will not show as image.

Now we can put our code in function and save

#r “System.Drawing”

using System.Drawing;

using System.IO;

public
static
void Run(Stream myBlob, string name, TraceWriter log, Stream outputBlob)

{

log.Info($”C# Blob trigger function Processed blobn Name:{name} n Size: {myBlob.Length} Bytes”);

Image myImage = Image.FromStream(myBlob);


// Assumes myImage is the PNG you are converting


using (var b = new Bitmap(myImage.Width, myImage.Height))

{

b.SetResolution(myImage.HorizontalResolution, myImage.VerticalResolution);


using (var g = Graphics.FromImage(b))

{

g.Clear(Color.White);

g.DrawImageUnscaled(myImage, 0, 0);

}


// Now save b as a JPEG like you normally would


using(MemoryStream stream = new MemoryStream())

{

b.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);


byte[] byteArray = stream.ToArray();

outputBlob.Write(byteArray, 0, byteArray.Count());

}

}

}

I will explain code shortly let us test it first, upload a file to the images container, this will trigger the function or you can trigger it manually by passing the container/resource (in our case images/debug.png) in test tab | request body | click Run


Now once the function is triggered verify that the new file is created.


Let us explain the code now.

#r “System.Drawing”

This line to include the assembly, which is not available by default by function apps.

By default below assemblies are available to use in function apps.

  1. mscorlib
  2. System
  3. System.Core
  4. System.Xml
  5. System.Net.Http
  6. Microsoft.Azure.WebJobs
  7. Microsoft.Azure.WebJobs.Host
  8. Microsoft.Azure.WebJobs.Extensions
  9. System.Web.Http
  10. System.Net.Http.Formatting

Following namespaces are automatically added

  1. System
  2. System.Collections.Generic
  3. System.IO
  4. System.Linq
  5. System.Net.Http
  6. System.Threading.Tasks
  7. Microsoft.Azure.WebJobs
  8. Microsoft.Azure.WebJobs.Host

If you have any custom assembly you can add it as well, even you can include nugget packages.

Microsoft may increase the number of assemblies or namespaces available by default by the time you study this post take look
For more info.

Next we have using statements, and then signature, myBlob will give you blob object as stream and outputBlob is stream object to write to container. Next is some logic for converting png to jpg, the important part is, even though the outputBlob is stream but it will give you error if you directly assign another stream to it. To overcome that we have converted file to bytes array and saved it by using Write method.

using System.Drawing;

using System.IO;

public
static
void Run(Stream myBlob, string name, TraceWriter log, Stream outputBlob)

{

log.Info($”C# Blob trigger function Processed blobn Name:{name} n Size: {myBlob.Length} Bytes”);

Image myImage = Image.FromStream(myBlob);


// Assumes myImage is the PNG you are converting


using (var b = new Bitmap(myImage.Width, myImage.Height))

{

b.SetResolution(myImage.HorizontalResolution, myImage.VerticalResolution);


using (var g = Graphics.FromImage(b))

{

g.Clear(Color.White);

g.DrawImageUnscaled(myImage, 0, 0);

}


// Now save b as a JPEG like you normally would


using(MemoryStream stream = new MemoryStream())

{

b.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);


byte[] byteArray = stream.ToArray();

outputBlob.Write(byteArray, 0, byteArray.Count());

}

}

}

Do not limit yourself with just blob trigger function Azure have a whole bunch of Trigger and they are adding more day by day.

Congratulations! You have successfully completed this series on Azure Development.

Upcoming next series on infrastructure of Azure. I hope you are enjoying your stay at this blog.

 

Azure App Service, Azure Blob, Azure Cloud, Azure Function, Azure Storage, Cloud Computing

How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2: Part 10

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

 

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2

 

Azure Function is just a piece of code which will run upon a trigger or any supported events like, when adding something to container in Azure Storage, or on when new item is added in Queue, or you can schedule to run at a particular time. If you have used Web Jobs on Azure, then think of Azure Functions as an evolution of the web Jobs, which support integration with wide variety of Azure services, such as Azure cosmosDB, Azure EventHub, Azure Mobile Apps, Azure Storage and many more, which enable us to take action when a particular event is triggered. Azure Function is implementing the concept known server less computing and is used to build Nano-services.

Below is a list of supported triggers for Azure functions.

In this post, we will learn about Blob Trigger since we have learned to upload images as block blobs so it is obligatory to extend the functionality using Azure Functions. We will create a jpg version of uploaded png image on blob trigger and store it back in blob storage under same container, although there is an option to store the resultant image to another container as well.

Before we begin, let us take a look at pricing aspect of Azure functions, there are two basic offering, one is to use your existing App Service Plan to run functionApp in it, and another way is to calculate the number of times the function executes and they have a specific name for this called consumption plan. Since we already have App Service Plan so we are going to use it to execute our functions. For pricing information.

 

Remember that consumption plan have many advantages over App service plan, as you are allowed to run about 400,000 GB-s execution time for free and if your function is not being used you do not need to pay any extra cost and for some reason if your function have to run 10 million time or experience exponential growth, then Azure will manage the resources behind the scene to make sure there are enough resources to execute your function on demand. On the other hand in App Service Plan you need to pay for App service Plan even if there is no execution of your function and you will manage the scaling and performance of the App service plan.

Create Function App

Go to Azure Portal | Add new | Search Function | choose Function App | click create

 

Figure 1 Create Function App

Fill fields, we are going to use our existing App Service Plan. We are using existing storage account as well; these were created by us earlier in this series. Click create, it should not take much time to create function app.

 

Figure 2 Create Function App with App Service Plan

Once created, go to resource. You will find the main overview similar to below: azure is continuously evolving and by the time, you read this below screen may have been changed.

 

Figure 3 Overview Azure Function

Click on “Functions”, you will see that we do not have any function as of yet, click on new function.

 

Figure 4 function listing

Create Blob Trigger

You can see that there are multiple trigger choices we have and Azure is keep adding more as days passing by. We are going to use Blob Trigger and we will be using C# as programming language.

 

Figure 5 Create New function

Now click on C# link in the Blob Trigger tile.

 

Figure 6 C# Blog trigger

Modify function name:

 

Figure 7 Modify function name

If you run the function now, you will get error as “Exception binding parameter ‘myBlob’. Microsoft.WindowsAzure.Storage: The remote server returned an error: (404) Not Found”. You can see it in logs window at the bottom.

We are getting this error because we have not configured the container path and the blob path in test window on the right does not exists. Let us do that now.

 

Figure 8 Run Function

As the post is getting long, I am going to extend it to part 2.

We will continue building our FunctionApp in next part How to use Azure Functions and trigger on new Item addition in Azure Storage using BlobTrigger 2/2

Azure App Service, Azure Blob, Azure Cloud, Cloud Computing

Azure storage data services types and How to store files in Azure Storage Account 2/2: Part 9

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

9. Azure storage data services types and how to store files in azure storage account 2/2

Now we will do the same upload process programmatically in visual studio, we will only focus on the relevant code, and we do not discuss any coding best practices as it is out of scope of this series, it is left on user’s own discretion.

Install WindowsAzure.Storage nugget package

Open your visual studio if you have not opened it already and open the solution with which we are working in this series. Right click on solution | Manage Nugget Packages | click on Browse | search for azure storage. Install latest stable version in our case it is 8.7.0 and accept the license. By the time you read this post, Microsoft may have released new version, so do not worry about it as all the code will still work.

Figure 5 Install Windows AZURE Storage nugget package

Open you Index controller and the code should look like below; all you need to do is modify the highlighted with your credentials to make it work.

Figure 6 Change settings for storage account container

Now to get the Blob service endpoint Go to Storage Account | Blob Service | under Primary blob service endpoint you will have the URL, copy it.

Figure 7 Blob service endpoint

For storage account and keys Go to your storage account | Access keys, although storage account can be retrieved from main overview window, but this view will give you both account name and keys in one location. Please remember that whoever have these keys, will be able to manipulate almost anything in the storage account level so you do not want to give these keys to everyone. Azure provides us SAS (shared access service) which is robust and should be used to limit access in production environments. You also have option to regenerate keys, which will invalidate the previous keys and will provide you new keys.

Figure 8 Blob service Storage account and keys

Coding in Visual Studio

In first Index method, all we are doing is listing the blob files in the container images and making a dictionary, which have image name as key and image URL as value, and we are passing it to view using ViewBag.files.

Figure 9 Get all files in container

In post Index method, we are checking for file first, if exists then we are creating reference to the blob container with file name, but you could also use GUID for your blob name. Note that if a blob/file exists in the container with particular name and you use the same name to upload the file again then existing file will be overwritten. We are also using file stream to mitigate the need to save the file on disk first, you can of course implement your logic accordingly.

Figure 10 Insert Files in container

Full code for home controller is as below:

Now let us move to the view part, it is very simple, we just added the code for form, which will post to index method of home controller and is multipart that means will contain file as post response. We create an input with type file and a submit button. Make sure that your input file name is the same as you have mentioned the parameter to post Index file, otherwise you will not get the file in response.

Figure 11 File Upload code in MVC

We also included the code to display file name and picture along with it.

Figure 12 View all images code

Full code for Index View as below:

Now run the application locally and upload a picture, you will see that the filename is encoded in html friendly name as it have spaces in it and images are shown. There are two pictures as we upload first picture right after we created storage account and the other one is uploaded programmatically.

Figure 13 Complete view of file upload

You can also verify the image upload by going to images container in storage account.

Figure 14 Image uploaded to images container

This code tutorial is just starting point; you can play with it and use this code to modify according to your needs. I hope this being information for you.

Coming up next How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger

Azure App Service, Azure Blob, Azure Cloud, Azure Storage, Cloud Computing

Azure storage data services types and how to store files in azure storage account 1/2 : Part 8

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

 

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2

 

Azure Function is just a piece of code which will run upon a trigger or any supported events like, when adding something to container in Azure Storage, or on when new item is added in Queue, or you can schedule to run at a particular time. If you have used Web Jobs on Azure, then think of Azure Functions as an evolution of the web Jobs, which support integration with wide variety of Azure services, such as Azure cosmosDB, Azure EventHub, Azure Mobile Apps, Azure Storage and many more, which enable us to take action when a particular event is triggered. Azure Function is implementing the concept known server less computing and is used to build Nano-services.

Below is a list of supported triggers for Azure functions.

In this post, we will learn about Blob Trigger since we have learned to upload images as block blobs so it is obligatory to extend the functionality using Azure Functions. We will create a jpg version of uploaded png image on blob trigger and store it back in blob storage under same container, although there is an option to store the resultant image to another container as well.

Before we begin, let us take a look at pricing aspect of Azure functions, there are two basic offering, one is to use your existing App Service Plan to run functionApp in it, and another way is to calculate the number of times the function executes and they have a specific name for this called consumption plan. Since we already have App Service Plan so we are going to use it to execute our functions. For pricing information.

Remember that consumption plan have many advantages over App service plan, as you are allowed to run about 400,000 GB-s execution time for free and if your function is not being used you do not need to pay any extra cost and for some reason if your function have to run 10 million time or experience exponential growth, then Azure will manage the resources behind the scene to make sure there are enough resources to execute your function on demand. On the other hand in App Service Plan you need to pay for App service Plan even if there is no execution of your function and you will manage the scaling and performance of the App service plan.

Create Function App

Go to Azure Portal | Add new | Search Function | choose Function App | click create

Figure 1 Create Function App

Fill fields, we are going to use our existing App Service Plan. We are using existing storage account as well; these were created by us earlier in this series. Click create, it should not take much time to create function app.

Figure 2 Create Function App with App Service Plan

Once created, go to resource. You will find the main overview similar to below: azure is continuously evolving and by the time, you read this below screen may have been changed.

Figure 3 Overview Azure Function

Click on “Functions”, you will see that we do not have any function as of yet, click on new function.

Figure 4 function listing

Create Blob Trigger

You can see that there are multiple trigger choices we have and Azure is keep adding more as days passing by. We are going to use Blob Trigger and we will be using C# as programming language.

Figure 5 Create New function

Now click on C# link in the Blob Trigger tile.

Figure 6 C# Blog trigger

Modify function name:

Figure 7 Modify function name

If you run the function now, you will get error as “Exception binding parameter ‘myBlob’. Microsoft.WindowsAzure.Storage: The remote server returned an error: (404) Not Found”. You can see it in logs window at the bottom.

We are getting this error because we have not configured the container path and the blob path in test window on the right does not exists. Let us do that now.

Figure 8 Run Function

As the post is getting long, I am going to extend it to part 2.

We will continue building our FunctionApp in next part How to use Azure Functions and trigger on new Item addition in Azure Storage using BlobTrigger 2/2