【问题标题】:Azure EventGrid (Blob Created) Function output streamAzure EventGrid(已创建 Blob)函数输出流
【发布时间】:2020-11-17 09:14:13
【问题描述】:

如果我创建一个 azure 函数 Blob Trigger,我可以定义 Blob 的输入流和我想写入一些数据的输出流,签名类似于下面。

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

是否可以在使用为创建 blob 时触发的 EventGrid 触发器定义类似的内容? 即

  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)
        {
            /* ... */
        }

【问题讨论】:

    标签: c# azure-functions azure-blob-storage azure-eventgrid azure-blob-trigger


    【解决方案1】:

    可以为 blob 绑定到 CloudBlobContainer 而不是 Stream,后者为 blob 提供完整的存储 API。它看起来像这样:

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

    来自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
    

    【讨论】:

    • 我最终走上了这条路,我最终使用了 CludBlockBlob,并向后工作。
    【解决方案2】:

    当然你可以在 EvenGrid 触发函数中使用输出流。我在本地测试它。首先,我上传了一个名为“input.txt”的blob,内容为“input”。运行我的函数后,我上传另一个 blob(或删除另一个 blob)来触发触发器,然后将内容“input”添加到名为“test.txt”的 blob 中。

    这是我的代码。

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

    【讨论】:

    • 这里的问题是我有动态路径,这意味着我必须使用 {data.url} 作为 blob 路径。我无法像您一样操纵它来更新写入路径。当您使用 blob 触发器时,您可以使用“a/{b}/”之类的内容定义动态 url,据我所知,这不适用于 EventGridTriggers
    猜你喜欢
    • 2019-07-03
    • 2017-04-06
    • 2019-07-10
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多