简体   繁体   English

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

[英]Azure Function triggered by EventGrid on Blob Storage

I have followed the Microsoft tutorial to process event based on blob being created in Azure storage.我已经按照Microsoft 教程处理基于 Azure 存储中创建的 blob 的事件。

The event is firing but the event code to process the image is bypassed as the input stream parameter is not being populated by the EventGrid event.事件正在触发,但处理图像的事件代码被绕过,因为 EventGrid 事件未填充输入流参数。 This should be passing through the path of the blob (image file) to process.这应该通过 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 ..

The log each time the event fires is每次事件触发的日志是

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,

The tutorial works for v1 c# script function as you can see it mentions csx file when talking about function code. 该教程适用于v1 c#脚本函数,因为您在谈论函数代码时会提到csx文件 But now the project link points to v2 pre-compiled code, changes in the code could cause problem when we follow the tutorial strictly. 但是现在项目链接指向v2预编译的代码,当我们严格按照教程进行操作时,代码中的更改可能会导致问题。

Let's fix the inconsistency with two steps. 让我们通过两个步骤来解决不一致问题。

  1. The key point is function wasn't connected to the blob Storage account created in part1 , hence we got null input stream. 关键是功能未连接到part1中创建的blob存储帐户,因此输入流为空。

    Since we have created an app setting myblobstorage_STORAGE in this step , we only have to add it in our function code. 由于我们已经在此步骤中创建了一个设置myblobstorage_STORAGE的应用程序,因此我们只需将其添加到我们的功能代码中即可。

     public static async Task Run( [EventGridTrigger]EventGridEvent eventGridEvent, [Blob("{data.url}", FileAccess.Read, Connection = "myblobstorage_STORAGE")] Stream input, ILogger log) 
  2. In the same step, tutorial sets an app setting myContainerName for container thumbnails created in Blob Storage Account in part1 . 在同一步骤中,教程将为在part1中的Blob存储帐户中创建的容器thumbnails设置一个应用程序,设置myContainerName

    But in our code we can see it connects to the Storage account created for Function app with AzureWebJobsStorage and wants to get container name from app setting THUMBNAIL_CONTAINER_NAME . 但是在我们的代码中,我们可以看到它连接到使用AzureWebJobsStorage 为Function应用程序创建存储帐户 ,并且想要从应用程序设置THUMBNAIL_CONTAINER_NAME获取容器名称。

    The quick fix is to replace AzureWebJobsStorage and THUMBNAIL_CONTAINER_NAME , and set a constant for thumbnailWidth . 快速解决方案是替换AzureWebJobsStorageTHUMBNAIL_CONTAINER_NAME ,并为thumbnailWidth设置一个常量。

     private static readonly string BLOB_STORAGE_CONNECTION_STRING = Environment.GetEnvironmentVariable("myblobstorage_STORAGE"); ... var thumbnailWidth = 100; var thumbContainerName = Environment.GetEnvironmentVariable("myContainerName"); 

    Of course you could choose to add THUMBNAIL_WIDTH in Application settings of Azure portal. 当然,您可以选择在Azure门户的“应用程序”设置中添加THUMBNAIL_WIDTH

Republish and everything should work. 重新发布,一切正常。

数据将永远不会传递。“事件网格”事件将仅传递元数据,其中将包含您可以用来检索内容的Blob URI(如果需要)。

除了当前接受的答案 ( https://stackoverflow.com/a/53314953/816663 ),如果您的 Function 应用程序具有系统或用户分配的具有适当身份的身份,您还可以在不添加Connection参数的情况下使其工作存储帐户的 Blob 权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM