由 Blob 存储上的 EventGrid 触发的 Azure 函数

Azure Function triggered by EventGrid on Blob Storage

我已按照 Microsoft tutorial 处理基于在 Azure 存储中创建的 blob 的事件。

事件正在触发,但处理图像的事件代码被绕过,因为 EventGrid 事件未填充输入流参数。这应该是通过blob(图像文件)的路径进行处理。

 public static async Task Run(
        [EventGridTrigger]EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read)] Stream input,
        ILogger log)
    {
        try
        {
            log.LogInformation("Entered Thumbnail Function ..");

            if (input != null) 
            { //doesn't get to here ..

每次事件触发的日志是

2018-11-15T05:33:41.096 [Information] Executing 'Thumbnail' (Reason='EventGrid trigger fired at 2018-11-15T05:33:41.0781270+00:00' ..

2018-11-15T05:33:41.096 [Information] Entered Thumbnail Function

2018-11-15T05:33:41.096 [Information] Executed 'Thumbnail' (Succeeded, 

2018-11-15T05:33:41.096 [Information] Executing 'Thumbnail' (Reason='EventGrid trigger fired at 2018-11-15T05:33:41.0781270+00:00', 

2018-11-15T05:33:41.096 [Information] Entered Thumbnail Function

2018-11-15T05:33:41.096 [Information] Executed 'Thumbnail' (Succeeded,

永远不会传入数据。事件网格事件将仅传递元数据,其中将包含 blob URI,您可以在需要时使用它来检索内容。

本教程在讨论函数代码时适用于 v1 c# script function as you can see it mentions csx file。但是现在项目link指向v2预编译代码,严格按照教程修改代码可能会出问题。

让我们通过两个步骤解决不一致问题。

  1. 关键是函数没有连接到 part1 中创建的 blob 存储帐户,因此我们得到了空输入流。

    由于我们在 this step 中创建了一个应用程序设置 myblobstorage_STORAGE,我们只需将其添加到我们的功能代码中即可。

    public static async Task Run(
        [EventGridTrigger]EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read, Connection = "myblobstorage_STORAGE")] Stream input,
        ILogger log)
    
  2. 在同一步骤中,教程为在 part1 的 Blob 存储帐户中创建的容器 thumbnails 设置了一个应用设置 myContainerName

    但在我们的代码中,我们可以看到它使用 AzureWebJobsStorage 连接到 Storage account created for Function app,并希望从应用程序设置 THUMBNAIL_CONTAINER_NAME 中获取容器名称。

    快速修复是替换 AzureWebJobsStorageTHUMBNAIL_CONTAINER_NAME,并为 thumbnailWidth.

    设置一个常量
    private static readonly string BLOB_STORAGE_CONNECTION_STRING = Environment.GetEnvironmentVariable("myblobstorage_STORAGE");
    ...
    var thumbnailWidth = 100;
    var thumbContainerName = Environment.GetEnvironmentVariable("myContainerName");
    

    当然你可以选择在 Azure 门户的应用程序设置中添加 THUMBNAIL_WIDTH

重新发布,一切正常。

除了当前接受的答案 () 之外,如果您的 Function 应用程序的系统或用户分配的标识具有存储帐户的适当 Blob 权限。