1

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)
        {
            /* ... */
        }
Heinrich
  • 2,144
  • 3
  • 23
  • 39

2 Answers2

3

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
Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
0

of course you can use output stream in EvenGrid Trigger Function. I test it locally. Firstly, I upload a blob named "input.txt" with the content "input". After run my function, I upload another blob (or delete another blob) to fire the trigger, then the content "input" is added to the blob named "test.txt".

Here is my code.

 [FunctionName("Function1")]
        public static async System.Threading.Tasks.Task RunAsync([EventGridTrigger] EventGridEvent eventGridEvent,
           [Blob("sample-items/input.txt", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream input,
           [Blob("sample-items/test.txt", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream output)
        {
            await input.CopyToAsync(output);
                    ......
         }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • The issue is here is that I have dynamic path, meaning I have to using {data.url} as the blob path. Which I cannot manipulate to update the write path as you have. When you are using a blob trigger you can define a dynamic url with stuff like "a/{b}/" which as far as I can tell doesn't work with EventGridTriggers – Heinrich Jul 29 '20 at 21:46