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:
- Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
- Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
- Remote debugging App Service using visual Studio, monitoring and configuring alerts
- Diagnostic logs, live stream, process explorer and KUDU
- How to use Azure SQL Database in Dot.net Applications
- How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
- How to use Visual Studio Team Service to do continuous Integration and continuous delivery
- Azure storage data services types and how to store files in azure storage account 1/2
- Azure storage data services types and how to store files in azure storage account 2/2
- How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
- 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.
-
mscorlib
-
System
-
System.Core
-
System.Xml
-
System.Net.Http
-
Microsoft.Azure.WebJobs
-
Microsoft.Azure.WebJobs.Host
-
Microsoft.Azure.WebJobs.Extensions
-
System.Web.Http
-
System.Net.Http.Formatting
Following namespaces are automatically added
-
System
-
System.Collections.Generic
-
System.IO
-
System.Linq
-
System.Net.Http
-
System.Threading.Tasks
-
Microsoft.Azure.WebJobs
-
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.