Search code examples
c#functionazureinputstreamazure-blob-storage

Azure Functions - Blob Stream Dynamic Input bindings


I'm running a C# function on azure which needs to take in files from a container. The only problem is that the paths to the input files are going to be (potentially) different each time, and the number of input files will vary from 1 to about 4 or 5. Accordingly I can't just use the default input blob bindings as far as I'm aware. My options are give the container anonymous access and just grab the files through the link or figure out how to get dynamic input bindings.

Does anyone know how to declare the path for the input blob stream at runtime (in the C# code)?

If it helps I've managed to find this for dynamic output bindings

using (var writer = await binder.BindAsync<TextWriter>(
                  new BlobAttribute(containerPath + fileName)))
    {
        writer.Write(OutputVariable);
    }

Thanks in advance, Cuan


Solution

  • For dynamic output bindings, you could leverage the following code snippet:

    var attributes = new Attribute[]
    {    
        new BlobAttribute("{container-name}/{blob-name}"),
        new StorageAccountAttribute("brucchStorage") //connection string name for storage connection
    };
    using (var writer = await binder.BindAsync<TextWriter>(attributes))
    {
        writer.Write(userBlobText);
    }
    

    enter image description here

    Note: The above code would create the target blob if not exists and override the existing blob if it exists. Moreover, if you do not specify the StorageAccountAttribute, your target blob would be create into the storage account based on the app setting AzureWebJobsStorage.

    Additionally, you could follow Azure Functions imperative bindings for more details.

    UPDATE:

    For dynamic input binding, you could just change the binding type as follows:

    var blobString = await binder.BindAsync<string>(attributes);
    

    Or you could set the binding type to CloudBlockBlob and add the following namespace for azure storage blob:

    #r "Microsoft.WindowsAzure.Storage"
    using Microsoft.WindowsAzure.Storage.Blob;
    
    CloudBlockBlob blob = await binder.BindAsync<CloudBlockBlob>(attributes);
    

    Moreover, more details about the operations for CloudBlockBlob, you could follow here.