Search code examples
c#azure-functionsazure-blob-storageazure-eventgridazure-blob-trigger

Azure EventGrid (Blob Created) Function output stream


If I create an azure function Blob Trigger, I can define the both an input stream of the blob, and an output stream to which I would like to write some data, the signature being similar to below.

public async Task RunAsync(
            [BlobTrigger(BlobPath, Connection = "FileStorage")]
            Stream inputStream,
            [Blob(OutputBlobPath, FileAccess.Write, Connection = "FileStorage")]
            Stream outputStream,
            /* other fields removed for brevity */
            ) 
{ 
    /* ... */
}

Is it possible to define something similar when using an EventGrid trigger that fires for blob being created? i.e.

  public async Task RunAsync(
            [EventGridTrigger] EventGridEvent eventGridEvent, 
            [Blob("{data.url}", FileAccess.Read, Connection = "FileStorage")] Stream input,
/* is something like this possible -> */ [Blob(?, FileAccess.Write, Connection = "FileStorage")] Stream output)
        {
            /* ... */
        }

Solution

  • It's possible to bind to a CloudBlobContainer rather than a Stream for a blob, which provides the full storage API for blobs. It would look something like this:

    public static async Task Run(
        [EventGridTrigger] EventGridEvent eventGridEvent,
        [Blob(/* container params */)] CloudBlobContainer blobContainer,
        ILogger log)
    {
       // Use blobContainer to read/write blobs in container
    }
    

    From the Azure docs:

    You can bind to the following types to write blobs:
    
    TextWriter
    out string
    out Byte[]
    CloudBlobStream
    Stream
    CloudBlobContainer
    CloudBlobDirectory
    ICloudBlob
    CloudBlockBlob
    CloudPageBlob
    CloudAppendBlob