Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind to ICloudBlob or some other (not string) type

I've been trying to create a Azure function being triggered when I add a image to a container on my blob storage account.

The only thing that seems to work, is when I have a string parameter, but the files are images, so I have no use for a string containing the image data.

So I've been trying each and every example I can find online (not that many), and now I've tried the samples from the azure webjobs sdk - this isn't wokring either. So either I'm stupid, which I feel right now, I'm missing something obvious?

There are some of the errors I get:

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.thumbnailgenerator'. Microsoft.Azure.WebJobs.Host: Can't bind BlobTrigger to type 'Microsoft.WindowsAzure.Storage.Blob.ICloudBlob'.

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.thumbnailgenerator'. Microsoft.Azure.WebJobs.Host: Can't bind BlobTrigger to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob'.

Right now the function I'm trying out, is the one given in the sample above, and like so many others I've tried, it's not working with anything but strings.

So how should I create the function (with C#) and the function.json file, to make it work with a blob in and preferable a string in with the name of the blob. Either that or blob in and one out, where the name of the out blob is in a different container and the name is prefixed with a hardcoded string.

This is what I got now, and it's not running:

function.json

{
  "bindings": [
{
  "type": "blobTrigger",
  "name": "blob",
  "direction": "in",
  "path": "kitimages/{name}.{ext}"
},
 {
        "type": "blob",
        "name": "output",
        "direction": "inout",
        "path": "thumbnails/{name}_300_200.{ext}"
    }  ],
  "disabled": false
}

run.csx

#r "Microsoft.WindowsAzure.Storage"
using System;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage.Blob;
public static void Run(CloudBlockBlob blob, CloudBlockBlob output, TraceWriter log)
{
    log.Info($"C# Blob trigger function processed a blob. Blob={blob.Name}");
}

EDIT: Take a look here for the final solution to my question: Getting work done in the cloud

like image 872
Steen Tøttrup Avatar asked Oct 07 '16 19:10

Steen Tøttrup


2 Answers

We need to improve the template here, this is a common pitfall you've run into (sorry about that!). We're fixing, see the GitHub issue: Make it easier for users to get started with binary blob triggers.

There's a built-in template that binds to streams. Go to New Function and select C# for language and Samples for Scenario.

For a more advanced sample that uses CloudBlockBlob bindings (which requires the InOut binding direction that is not yet documented), see the Functions sample in ContosoMoments: DeleteImages Function.

Note that you can browse all the templates in the GitHub repo: https://github.com/Azure/azure-webjobs-sdk-templates.

enter image description here

like image 191
lindydonna Avatar answered Oct 26 '22 23:10

lindydonna


For anyone else stumbling upon this while seemingly having a correct setup as per above:

I got this message because I had a reference to WindowsAzure.Storage in my project.json file. Perhaps because it was referring to an older version (8.1.1) of the library.. I don't know. Removing it made my function work. Since it's a supported DLL you should just import it using #r..

like image 42
bech Avatar answered Oct 26 '22 23:10

bech