locked
Dynamic Container Path to Output blob (Image resize) RRS feed

  • Question

  • Hello,

    How do I make my blob output write to a custom container path? In the Intput/Output binding template, it's allowing me to specific paths like "mycontainer/{name}"  or "mycontainer/{rand-guid}" something like this for the blob object. 

    But I would like to write my out-put blob to a custom container, the name of the container will be picked up from incoming blob's meta data. 

    When I searched the web, I could see I need to use IBinder but it's not clear.  I'm not sure how to integrate this into my Azure function. 

    Here's the code 

    public static void Run(Stream inputblob,Binder binder, string name, ILogger log, Stream outputBlob)
    {

    using (Image<Rgba32> image = Image.Load(inputblob))
    {
    image.Mutate(x => x
    .Resize(new ResizeOptions{
    Mode = ResizeMode.BoxPad,
    Size = new Size(1200, 627)
    }).BackgroundColor(new Rgba32(0,0,0)));
    string ext = name.Substring(name.Length-4);

    image.Save(outputBlob);

    }

    }

    My questions are,

    1. How do I mention a dynamic path (Container) name for the outputBlob

    2. Even when we use IBinder, we have to keep the outputbinding that's generated through the GUI wizard right?

    In function.json file,

    The output binding is like this :

    {
    "type": "blob",
    "name": "outputBlob",
    "path": "thumbnails/{name}",
    "connection": "STORAGE_CONN_STRING",
    "direction": "out"

    }

    Should I change any of it while using IBinder?

    Kindly help. I'm stuck with this for many days now.


    :#

    Saturday, August 10, 2019 10:37 AM

Answers

  • You don't require the output binding in function.json any more if you are using runtime binding.

    You can refer the multiple attribute binding example to achieve what you are trying.

    Here is the code from the example for reference

    public static class IBinderExampleMultipleAttributes
    {
        [FunctionName("CreateBlobInDifferentStorageAccount")]
        public async static Task RunAsync(
                [QueueTrigger("myqueue-items-source-binder2")] string myQueueItem,
                Binder binder,
                ILogger log)
        {
            log.LogInformation($"CreateBlobInDifferentStorageAccount function processed: {myQueueItem}");
            var attributes = new Attribute[]
            {
            new BlobAttribute($"samples-output/{myQueueItem}", FileAccess.Write),
            new StorageAccountAttribute("MyStorageAccount")
            };
            using (var writer = await binder.BindAsync<TextWriter>(attributes))
            {
                await writer.WriteAsync("Hello World!!");
            }
        }
    }

    Based on your requirement, you would change the path to the BlobAttribute constructor to be dynamic.


    Monday, August 12, 2019 6:42 AM

All replies

  • You don't require the output binding in function.json any more if you are using runtime binding.

    You can refer the multiple attribute binding example to achieve what you are trying.

    Here is the code from the example for reference

    public static class IBinderExampleMultipleAttributes
    {
        [FunctionName("CreateBlobInDifferentStorageAccount")]
        public async static Task RunAsync(
                [QueueTrigger("myqueue-items-source-binder2")] string myQueueItem,
                Binder binder,
                ILogger log)
        {
            log.LogInformation($"CreateBlobInDifferentStorageAccount function processed: {myQueueItem}");
            var attributes = new Attribute[]
            {
            new BlobAttribute($"samples-output/{myQueueItem}", FileAccess.Write),
            new StorageAccountAttribute("MyStorageAccount")
            };
            using (var writer = await binder.BindAsync<TextWriter>(attributes))
            {
                await writer.WriteAsync("Hello World!!");
            }
        }
    }

    Based on your requirement, you would change the path to the BlobAttribute constructor to be dynamic.


    Monday, August 12, 2019 6:42 AM
  • Hi Conquis1 - Hope my response helps.
    Monday, August 19, 2019 1:41 PM
  • it does. thanks

    :#

    Monday, September 2, 2019 8:07 AM