BobbyA
BobbyA

Reputation: 2260

How do you reference a blob from an Azure Timer Trigger Function?

I have an Azure Timer Trigger Function that should do some calculations and write the results to a json file in a pre-existing blob. How do I reference the pre-existing blob from within the Timer Triggered function?

I can't seem to find any documentation that provides a code sample. Can someone provide one?

Upvotes: 1

Views: 1309

Answers (1)

BobbyA
BobbyA

Reputation: 2260

First, you need to update your function.json configuration file, to bind the blob to the CloudBlockBlob instance you'll be using in your .csx code. You can edit it in Azure Portal via the "Integrate" option (the one with the lighting icon) under your function, in the Function Apps menu. On the top right of that page is a link that reads "Advanced Editor". Clicking that link will take you to your funciton's function.json file:

enter image description here

You'll see a JSON array named "bindings" that contains a JSON object that configures your timer. You'll want to add another JSON object to that array to bind your blob to a CloudBlockBlob instance that you'll be referencing in your function. Your function.json file will look something like this:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "type": "blob",
      "name": "myBlob",
      "path": "your-container-name/your_blob_filename.json",
      "connection": "AzureWebJobsStorage",
      "direction": "inout"
    }
  ],
  "disabled": false
}

Now you just need to update your function's Run method's signature. It looks like this by default:

public static void Run(TimerInfo myTimer, TraceWriter log)

Add your blob variable to the end of that signature (and also add the necessary includes):

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

public static void Run(TimerInfo myTimer, TraceWriter log, CloudBlockBlob myBlob)

And you're all set! "myBlob" is bound to the blob "your_blob_filename.json" in your "your-container-name" container.

Upvotes: 4

Related Questions