Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions - Blob Stream output binding

I've created an Azure function with a blob storage trigger - I want to process a file and then dump the file out to another blob storage container.

In the simplest case I suppose it would look like this:

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

These are my bindings:

{
  "bindings": [
    {
      "name": "blob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "input/{name}",
      "connection": "wlimportstaging_STORAGE"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "original/{name}",
      "connection": "wlimportstaging_STORAGE"
    }
  ],
  "disabled": false
}

I've read from the documentation that if you return a POCO it will serialize it as JSON.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#output-usage

This seems to suggest you can output to a stream - I just seem to be getting:

Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'System.IO.Stream&

Please assist!

like image 809
Nosmadas Avatar asked Feb 05 '23 15:02

Nosmadas


2 Answers

You an also do

public static async Task Run(Stream myBlob, string name, Stream outputBlob, TraceWriter log)
{
    await myBlob.CopyToAsync(outputBlob);
}
like image 71
CodeClimber Avatar answered Mar 13 '23 04:03

CodeClimber


This worked fine. If there is a better approach I would be really interested.

public static async Task Run(Stream blob, string name, Stream outBlob, TraceWriter log)
{
    using (MemoryStream ms = new MemoryStream())
    {
        blob.CopyTo(ms);
        var byteArray = ms.ToArray();
        await outBlob.WriteAsync(byteArray, 0, byteArray.Length);
    }
}
like image 24
Nosmadas Avatar answered Mar 13 '23 03:03

Nosmadas