0

How do we bind to properties inside of a blob?

My bindings:

    public static async Task Run(
        [BlobTrigger("%triggerContainer%/{name}")] Stream myBlob,
        [Blob("%triggerContainer%/{name}", Read)]  CloudBlockBlob @in,
        [Blob("%outputContainer%/{name}", Write)] CloudBlockBlob @out,
        string name, ILogger log)
    {

I'd like to be able to change the blobtrigger type to a POCO:

[BlobTrigger("%triggerContainer%/{name}")] MyPoco myBlob

Where MyPoco might be something like this:

public class MyPoco
{
   public string id {get;set;}
   public string filename {get;set;}
}

From within the function I'd like to be able to do something like this:

var thisId = myBlob.id;
var thisfileName = myBlob.filename;

How do we bind to the actual contents of the blob with a POCO object?

According to this:

JavaScript and Java functions load the entire blob into memory, and C# functions do that if you bind to string, Byte[], or POCO.

1
  • Interesting - I have just tried and get an error like: The 'Function1' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Host: Can't bind BlobTrigger to type 'FunctionApp6.MyPoco'. - is that what you get? Commented Mar 4, 2020 at 11:00

1 Answer 1

1

First write an IExtensionConfigProvider that registers converters as needed,

internal class CustomBlobConverterExtensionConfigProvider : IExtensionConfigProvider
{
    public void Initialize(ExtensionConfigContext context)
    {
        context.AddConverter<Stream, MyPoco>(s =>
        {
            // read and convert
            return new MyPoco();
        });

        context.AddConverter<ApplyConversion<MyPoco, Stream>, object>(p =>
        {
            MyPoco value = p.Value;
            var stream = p.Existing;

            TextWriter writer = new StreamWriter(stream);
            // write the value to the stream
            writer.FlushAsync().GetAwaiter().GetResult();

            return null;
        });
    }
}

Then, you register this config provider with the host builder on startup:

var builder = new HostBuilder()
    .ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices()
        .AddAzureStorage()
        .AddExtension<CustomBlobConverterExtensionConfigProvider>();
    })

Then bind your BlobTrigger function to Mypoco:

public void BlobTrigger([BlobTrigger("test")] Mypoco item, ILogger logger)
{
    // process item
}

For more information, have a look of this doc.

Sign up to request clarification or add additional context in comments.

1 Comment

cha chiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing

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.