简体   繁体   中英

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.

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. This should be passing through the path of the blob (image file) to process.

 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. But now the project link points to v2 pre-compiled code, changes in the code could cause problem when we follow the tutorial strictly.

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.

    Since we have created an app setting myblobstorage_STORAGE in this step , we only have to add it in our function code.

     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 .

    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 .

    The quick fix is to replace AzureWebJobsStorage and THUMBNAIL_CONTAINER_NAME , and set a constant for 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.

Republish and everything should work.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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