TimerTrigger Azure Function to Upload to Azure Blob Daily

[toc]

Introduction

Working with Azure Function is always fun and with the help of other Azure Services, it gets even better. Here in this article, we are going to see how we can use the TimerTrigger of Azure Function to upload some file to the Azure Blob storage daily. The basic idea is to have job running every data at a specific time to perform the upload. I believe, this is enough for the introduction, let us start implementing them.

Background

If you are new to Azure Functions, I recommend you to read some related articles from here. The possibilities that you can do with Azure Function is countless, and here we are going to discuss one of them. We live in a world everything is automated, nobody is interested in doing any manual works. Writing an automated Job which runs everyday at a given time is never been easier with the help of Azure Function.

Prerequisites

  • You should have a valid Azure Subscription
  • You should have a running Azure Function
  • If you are not sure about how to create an Azure Function App, this post may help you.
  • You should have a valid Azure Storage Account

Azure Function and Azure Blob

Get the Storage Connection String

By default a new key with the name AzureWebJobsStorage will be created when you create an Azure Function in your Visual Studio Azure Function App. If it doesn’t you should create this in your local.settings.json file by getting the connection string from your storage account.

To get the connection string, go to your storage account and click on Access Keys under Settings blade. And then copy any one of the connection string listed. You can use this value to create the AzureWebJobsStorage key under Values in the local.settings.json file. You should also create this in the Azure Function configuration in the Azure Portal, as this local.settings.json file is just for local and it will not be pushed to the Azure Function when you publish your Functions.

Creating an Azure Function in Function App

To create a new Azure Function in the Azure Function App, right click on your project and then click on Add and then select New Azure Function. In the coming pop up you should be given an option to name your Azure Function. Once that is done you can choose what trigger you want to use, in this case I will select TimeTrigger.

Azure Function with Time Trigger

Once you click on Ok, a new Function will be created for you with the time interval of 5 minutes. The Time Trigger Function uses NCronTab library to intercept the CRON expression. This is the expression that you see under the Schedule option on the above image. A usual CRON express has the below fields in it.

If you see the code you can see that a function Run is been created for you, with the time interval you had set.

Now we can say our Azure Function to run everyday at 9.45 AM by changing the CRON expression.

You can learn more about the CRON expressions here.

Upload the Data to Azure Blob

Now that we have our Azure Function ready to be run everyday as we wanted. It is time to write some code to generate the data and upload it to the blob. Before we do that we should make sure that we have installed the required Nuget package Microsoft.Azure.Storage.Blob.

Install Microsoft.Azure.Storage.Blob

Now we can rewrite the complete Function code as below.

As you can see that, I am creating an instance of CloudStorageAccount and then create a blob client by calling the function CreateCloudBlobClient(). Once we get that, we get the blob container reference by using the function GetContainerReference() and then get the block blob and finally call the UploadFromStreamAsync() function to upload the stream data.

The line var stream = getTheImportedData.ConvertToStream(); is to get the stream data from an API, which returns a JSON and then I convert it to a stream by calling an extension method.

Here EnviornmentVariables.StorageConnectionString is part of a shared static class which returns the AzureWebJobsStorage we set in the local.settings.json file.

Output

Now let us run our function in local and test. You should see an output as preceding.

Time Trigger Azure Function Output

Conclusion

Wow!. Now we have learned,

  • about Azure Function and setting up the same
  • about Time Trigger in Azure Function
  • about CRON expressions in Azure Function
  • how to upload data to Azure blob using Azure Function

You can always ready my Azure articles here.

Your turn. What do you think?

Thanks a lot for reading. Did I miss anything that you may think which is needed in this article? Could you find this post as useful? Kindly do not forget to share me your feedback.

Kindest Regards
Sibeesh Venu

Leave a Reply

Your email address will not be published. Required fields are marked *

Serverless 360