Search code examples
azureazure-functionsazure-blob-storageazureservicebus

How to write to blob storage as output binding using azure function with Service Bus Trigger


I have a azure function with Service Bus Topic Trigger. Every time the service bus topic receives a message, this function gets triggered.

enter image description here I added the output binding to Blob Storage using:

enter image description here

But when the function runs, I do not see any output in blob storage. I am sure I am missing some basic code.

I saw multiple samples but every sample is using Blob Storage trigger.


Solution

  • I have reproduced in my environment and got expected results and I followed the below process:

    In my run.csx:

    using System;
    using System.Threading.Tasks;
    
    public  static  void Run(string myQueueItem, ILogger log,ICollector<string> outputQueueItem)
    {
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    outputQueueItem.Add(myQueueItem);
    }
    

    enter image description here

    In my Integration i added output as below:

    enter image description here

    In function.json:

    {
    "bindings": [
    {
    "name": "myQueueItem",
    "connection": "Con",
    "queueName": "rithwik",
    "direction": "in",
    "type": "serviceBusTrigger"
    },
    {
    "name": "outputQueueItem",
    "direction": "out",
    "type": "queue",
    "connection": "AzureWebJobsStorage",
    "queueName": "outqueue"
    }
    ]
    }
    

    Sent message in Service bus queue as below:

    enter image description here

    In my stoarge account a new queue is created as below and I got the message too:

    enter image description here

    enter image description here

    So you need to add ouputQueueItem.Add inside the code and ICollector<string> outputQueueItem in parameters as my example. Now you will get output also as I have got.

    Edit:

    I have added few lines in my code please check:

    run.csx:

    using System;
    using System.Threading.Tasks;
    public  static  void Run(string myQueueItem, ILogger log,ICollector<string> outputQueueItem,TextWriter outputBlob)
    {
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    outputQueueItem.Add(myQueueItem);
    outputBlob.WriteLine(myQueueItem);
    }
    

    function.json:

    {
    "bindings": [
    {
    "name": "myQueueItem",
    "connection": "Con",
    "queueName": "rithwik",
    "direction": "in",
    "type": "serviceBusTrigger"
    },
    {
    "name": "outputQueueItem",
    "direction": "out",
    "type": "queue",
    "connection": "AzureWebJobsStorage",
    "queueName": "outqueue"
    },
    {
    "name": "outputBlob",
    "direction": "out",
    "type": "blob",
    "path": "outcontainer/{rand-guid}",
    "connection": "AzureWebJobsStorage"
    }
    ]
    }
    

    In integration:

    enter image description here

    Container and blob got created as below:

    enter image description here

    Inside container:

    enter image description here

    So, I am making few changes such as TextWriter as parameter and WriteLine inside code we can get expected results, hope this clears your doubt.