4

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

2 Answers 2

1

try the below code:

    string filename = string.Format("{0}/{1}_{2}.json", blobname,                         DateTime.UtcNow.ToString("ddMMyyyy_hh.mm.ss.fff"), Guid.NewGuid().ToString("n"));

    using (var writer = await binder.BindAsync<TextWriter>(
            new BlobAttribute(filename, FileAccess.Write)))
            {
                writer.Write(JsonConvert.SerializeObject(a_object));
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Please elaborate a little more than simply the code snippet alone.
0

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.

3 Comments

That's helpful for dynamic output, but how would I go about doing something similar for input blobs? My function takes in a queue item which it uses to figure out which input files it is going to use, but I haven't been able to find any documentation for declaring input paths at runtime
I updated my answer and added some code example, you could refer to them.
cannot convert from Attribute[] to Attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.