0
votes

I am having challenged time here understanding the output binding of azure functions. I have a function that triggers fine on a servicebus. What I need to do is get some data, and write it back to an csv file in blob storage. I can see you have an output blob binding. But it provides a stream, or textwriter as input. How does this work? I want to be able to control the name of the csv file create. Can anyone provide some input how I actually create a csv file, write to it, and generate a file name with the output binding? Or have I misunderstood the use of bindings? Or should I use the CloudBlobClient when creating new files?

[FunctionName("UniquePermissionsReporting")]                                                                     
public static void Run(
[ServiceBusTrigger("spo-governance-report-permissions", AccessRights.Manage, Connection = "UniquePermisionsQueueConnStr")]string myQueueItem,
[Blob("unique-permission-reports/{name}", FileAccess.Write,Connection = "BlobStorageConnStr")]Stream CsvContainer,
TraceWriter log)
{

}
1

1 Answers

2
votes

Simply speaking, we need to specify the blob name and write content. Two ways for you to refer.

1st is to use CloudBlobContainer as output binding type.

[FunctionName("UniquePermissionsReporting")]                                                                     
public static void Run(
[ServiceBusTrigger("spo-governance-report-permissions", AccessRights.Manage, Connection = "UniquePermisionsQueueConnStr")]string myQueueItem,
[Blob("unique-permission-reports", FileAccess.Read, Connection ="BlobStorageConnStr")] CloudBlobContainer blobContainer,
TraceWriter log)
{
        string blobName = "test.csv";
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference($"{blobName}");
        // ensure the csv content type if necessary
        blob.Properties.ContentType = "text/csv";
        // use Upload* method according to your need
        blob.UploadText("content");
}

2nd is to work with Binding a runtime. Use imperative binding pattern(Binder) to bind output bindings to TextWriter or Stream on-the-fly. Note the Function is async as we use *Async method inside.

[FunctionName("UniquePermissionsReporting")]                                                                     
public static async Task Run(
[ServiceBusTrigger("spo-governance-report-permissions", AccessRights.Manage, Connection = "UniquePermisionsQueueConnStr")]string myQueueItem,
Binder binder,
TraceWriter log)
    {
        string blobName = "test.csv";
        var attributes = new Attribute[]
        {
            new BlobAttribute($"unique-permission-reports/{blobName}", FileAccess.Write),
            new StorageAccountAttribute("BlobStorageConnStr")
        };
        using (var writer = await binder.BindAsync<TextWriter>(attributes))
        {
            await writer.WriteAsync("Content");
        }
    }