Azure Functions - Blob Stream output binding

10,126

Solution 1

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);
    }
}

Solution 2

You an also do

public static async Task Run(Stream myBlob, string name, Stream outputBlob, TraceWriter log)
{
    await myBlob.CopyToAsync(outputBlob);
}
Share:
10,126
Nosmadas
Author by

Nosmadas

Updated on June 17, 2022

Comments

  • Nosmadas
    Nosmadas almost 2 years

    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://docs.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!

  • mathewc
    mathewc over 7 years
    Assuming you need to operate on the raw bytes in the stream, then yes this is probably the best approach.
  • Raas Masood
    Raas Masood over 5 years
    what if i want to save bytearray as jpeg image on blob