Silly John
Silly John

Reputation: 1704

Azure Functions - To write events from eventhub to Blob Storage

I am trying to move the events from event hub to the Blob storage. I have created trigger on the eventhub to trigger whenever a message comes to event hub. Also, i have configured outputs as Blob Storage. Right now, what i am doing within the function is:

public static void Run(string myEventHubMessage, out string outputBlob, TraceWriter log)
{
    outputBlob = myEventHubMessage;
}

This will create a new blob in the container mentioned in the Ouputs configuration. But, I want is to create a blob with a specified name based on the data present in the eventhub message and need to set the content type and other metadata while saving the data to Azure Blob. Can someone help how to do this?

Regards,

John

Upvotes: 0

Views: 1686

Answers (2)

johni
johni

Reputation: 5568

You might no longer need to do that. Event hub now supports piping to blob storage out of the box.

Upvotes: 2

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35134

There are several possibilities to configure your output binding.

If you need to set the Blob path based on event hub message properties, you can declare your strongly typed message

public class MyEvent
{
    public string SomeName { get; set; }
    // more properties
}

then bind it declaratively to blob path, e.g.

{
  "type": "blob",
  "name": "outputBlob",
  "path": "mycontainer/{SomeName}.json",
  "connection": "...",
  "direction": "out"
},

and modify the function accordingly

public static void Run(MyEvent myEventHubMessage, out MyEvent outputBlob)
{
    outputBlob = myEventHubMessage;
}

If you need more advanced calculation to determine the output path, you can remove declarative output binding from function.json and use imperative binding:

public static async Task Run(string myEventHubMessage, Binder binder)
{
    var path = ...;
    using (var writer = binder.Bind<TextWriter>(new BlobAttribute(path)))
    {
        writer.Write(myEventHubMessage);
    }
}

If you need to set more properties of Blob, bind to ICollector<CloudBlockBlob>

var collector = binder.Bind<ICollector<CloudBlockBlob>>(new BlobAttribute(path)));
collector.Add(new CloudBlockBlob { ... });

You should play with all those options to identify which scenario works for you.

Upvotes: 1

Related Questions