Creating an Azure function that triggers when a blob is uploaded

Parts to this series:

  1. Creating an Azure function that triggers when a blob is uploaded (this one)
  2. Creating an Azure function that triggers when a blob is uploaded - Creating and running in Visual Studio 2017
  3. Creating an Azure function that triggers when a blob is uploaded - Sending an email via Office 365
  4. Creating an Azure function that triggers when a blob is uploaded - Publishing to Azure from Visual Studio 2017

I've recently put some thought to backing up some files to Azure, like database backups of the database that sits behind this website. Because this is triggered by a Windows scheduled task that runs the PowerShell script that performs the backup and pushes it into Azure, there's no "positive" feedback that the file has definitely gone into Azure unless I look. Even if I added a step to the powershell script to send a notification on success/failure, a "dead" machine would result in no notification. There are a couple of ideas I'm toying around with:

  1. Generate an email to me when a file gets put into storage
  2. Generate an SMS to me (using twilio) when a file gets put into storage

Unfortunately, both of these would only be a +ve notification, there would be nothing that happens if the machine making the backup is unable to get it to Azure, so whilst it'd be a bit more robust, it'll still not completley fill the gap. In the interim, something is better than nothing, so the first step is to create an Azure function that reacts to a blob being added to storage.

Creating the Function App

To do this, I'm going to create it all via the Azure Portal, the first step is to create the Function App:

  1. Log into the Azure portal
  2. Click "+ New" on the left-hand side
  3. Search for, and choose, "Function App"
  4. Click "Create"

There's nothing to enter into the UI, yet, but clicking "Create" does take you immediately through to a form to fill-in with the details of your "Function App" which can contain many functions. Here you can give the app a name, assign it to a resource group, decide where it'll be hosted, basically the usual stuff for creating most things in Azure. As noted here, you cannot use blob-only storage accounts with Azure functions.

Once you click on "Create" on the second screen, having filled out all the details of your new Function App, you'll get the "Deployment in progress..." notification and need to wait a few minutes

Once that's done, you can go back to the "All resources" view in your dashboard, find your Function App and then click on it to bring up the details - you did give it a name that makes it easy to find, right?

Creating a function that reacts to a blob upload

The next thing to do is actually create a function that reacts to a blob being uploaded. If you let the Azure portal do all of the heavy lifting, you're about two minutes away from a proof-of-concept of this. Start by clicking on the "+" symbol next to "Functions" on the left-hand side. This brings up a screen that lets you choose to "Get started quickly with a premade function" where you choose a scenario and a coding language, or "Get started on your own". I've opted to go down the latter route by clicking on the "Custom function" link. Even this doesn't throw you to a blank screen waiting for code to be entered as you're then presented with a choice of templates:

I've highlighted "BlobTrigger" as the one I'm going to use, so click on that. You'll then get asked to give it a name, I've chosen "FunctionThatTriggersOnBlobUpload", to choose the path that you want the storage trigger to trigger from and the Storage ccount connection to use. If the drop-down for "Storage account connection" doesn't contain the storage account you want to use, click "new" just to the right of it to choose the account and create an app setting that contains the connection string for it. The default path is "samples-workitems/{name}" which means that the function will trigger for files that are added to the "samples-workitems" container within the selected account. Make sure you're happy with the settings here and click "Create". You should now be presented with a screen that looks like this:

Now comes the interestinng bit, running the function and seeing what happens! I'm going to do this the lazy/easy way, just to prove its playing ball by clicking the "> Run" button and then manually uploading a file to Azure to watch the function react (without any of the funky stuff to send SMS, email, or anything else, just yet!). I've done this just to get the log streaming window to show (as for whatever reason, in the browser I'm using right now - Firefox - it doesn't show), so the first thing I'll see in there is an error!

2017-09-18T13:46:51  Welcome, you are now connected to log-streaming service.
2017-09-18T13:47:51  No new trace in the past 1 min(s).
2017-09-18T13:48:51  No new trace in the past 2 min(s).
2017-09-18T13:49:23.339 Function started (Id=65a0fc71-ec05-4a96-a7a7-8c3124681b48)
2017-09-18T13:49:23.638 Exception while executing function: Functions.FunctionThatTriggersOnBlobUpload. Microsoft.Azure.WebJobs.Host: One or more errors occurred. Exception binding parameter 'myBlob'. Microsoft.WindowsAzure.Storage: The remote server returned an error: (404) Not Found.
2017-09-18T13:49:23.811 Function completed (Failure, Id=65a0fc71-ec05-4a96-a7a7-8c3124681b48, Duration=467ms)

We can quite merrily ignore this error as it was down to running the function but not having a blob to pass in. If you upload a file to the storage account in question (remember to make sure you upload it to the container you specified earlier) you should see something that looks a little like this:

That shows that we've got a Fuction App setup that's reacting to a blob being uploaded, no matter how silly the name of the blob, which means that there's somewhere to start from with sending emails, SMSs, smoke signals, or whatever works to get an alert sent out when a file is uploaded.

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment