首页 > 解决方案 > Azure Function: How to add row to cloud table on blob creating?

问题描述

I'm developing an Azure Function which should add new line to an Azure table when new a new blob is added. The application has many containers in Blob Storage, and my Azure Function should process all blobs from all containers.

I tried to implement event getting with EventGrid, but it gives an error.

My Azure function:

#r "D:\home\site\wwwroot\BlobCreatedFunction\Microsoft.Azure.EventGrid.dll"
#r"D:\home\site\wwwroot\BlobCreatedFunction\Microsoft.WindowsAzure.Storage.dll"
    using Microsoft.Azure.EventGrid.Models;
    using Microsoft.WindowsAzure.Storage.Table;
    using System;
public class TemporaryBlobEntity : TableEntity
{
    public TemporaryBlobEntity(string partitionKey, string rowKey)
    {
        this.PartitionKey = partitionKey;
        this.RowKey = rowKey;
    }

    public string BlobUrl { get; set; }
    public DateTime BlobUploaded { get; set; }

}
public static TemporaryBlobEntity Run(EventGridEvent eventGridEvent, ILogger log)
{
    if (eventGridEvent.Data is StorageBlobCreatedEventData eventData)
    {
        log.LogInformation(eventData.Url);

        log.LogInformation(eventGridEvent.Data.ToString());

        var temporaryBlob = new TemporaryBlobEntity("blobs", eventData.Url)
        {
            BlobUrl = eventData.Url,
            BlobUploaded = DateTime.UtcNow
        };

        return temporaryBlob;
    }

    return null;
}

Here is my integration JSON:

{
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent",
      "direction": "in"
    },
    {
      "type": "table",
      "name": "$return",
      "tableName": "temporaryBlobs",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ]
}

In my Azure Function settings, I added the value for AzureWebJobsStorage.

When I press Run in the test section, logs show:

2019-07-08T13:52:16.756 [Information] Executed 'Functions.BlobCreatedFunction' (Succeeded, Id=6012daf1-9b98-4892-9560-932d05857c3e)

Looks good, but there is no new item in cloud table. Why?

Then I tried to connect my function with EventGrid topic. I filled new subscription form, selected "Web Hook" as endpoint type, and set subscriber endpoint at: https://<azure-function-service>.azurewebsites.net/runtime/webhooks/EventGrid?functionName=<my-function-name>. Then I got the following error message:

Deployment has failed with the following error: {"code":"Url validation","message":"The attempt to validate the provided endpoint https://####.azurewebsites.net/runtime/webhooks/EventGrid failed. For more details, visit https://aka.ms/esvalidation."}

As far as I can understand, the application needs some kind of request validation. Do I really need to implement validation in each of my azure functions? Or shoudl I use another endpoint type?

标签: azure.net-coreazure-functionsazure-eventgrid

解决方案


当您将 webhook 输入到事件网格中时,它会发送一个请求以验证您是否确实拥有该端点的权限。将函数连接到事件网格的最简单方法是从函数应用程序而不是事件网格刀片创建订阅。

在门户中打开功能,您应该会在顶部找到“添加事件网格订阅”的链接。即使 Functions 应用程序是在本地创建并发布到 Azure,因此代码不可见,链接仍然可用。 在此处输入图像描述

这将打开用于创建事件网格订阅的屏幕。不同之处在于,不是预先填充事件网格主题信息,而是为您预先填充 Web 挂钩信息。填写有关事件网格主题的信息以完成创建订阅。

在此处输入图像描述

如果您出于某种原因决定要实现验证响应,则可以通过检查消息的类型来执行此操作。

// Validate whether EventType is of "Microsoft.EventGrid.SubscriptionValidationEvent"
if (eventGridEvent.EventType == "Microsoft.EventGrid.SubscriptionValidationEvent")
{
    var eventData = (SubscriptionValidationEventData)eventGridEvent.Data;
    // Do any additional validation (as required) such as validating that the Azure resource ID of the topic matches
    // the expected topic and then return back the below response
    var responseData = new SubscriptionValidationResponse()
    {
        ValidationResponse = eventData.ValidationCode
    };


    if (responseData.ValidationResponse != null)
    {

        return Ok(responseData);
    }
}
else
{
    //Your code here
}

还有一个选项可以手动验证链接,方法是从验证消息中获取验证链接并在浏览器中导航到该链接。此方法主要适用于无法添加验证码的第三方服务。


推荐阅读