gpt4 book ai didi

Azure函数: How to add row to cloud table on blob creating?

转载 作者:行者123 更新时间:2023-12-02 08:00:47 26 4
gpt4 key购买 nike

我正在开发一个 Azure 函数,当添加新的 blob 时,它应该向 Azure 表添加新行。该应用程序在 Blob 存储中有许多容器,我的 Azure 函数应该处理所有容器中的所有 Blob。

我尝试使用 EventGrid 实现事件获取,但出现错误。

我的Azure功能:

#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;
}

这是我的集成 JSON:

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

在我的 Azure Function 设置中,我添加了 AzureWebJobsStorage 的值.

当我在测试部分中按运行时,日志显示:

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

看起来不错,但云表中没有新项目。为什么?

然后我尝试将我的函数与 EventGrid 主题连接起来。我填写了新的订阅表单,选择“Web Hook”作为端点类型,并将订阅者端点设置为:https://<azure-function-service>.azurewebsites.net/runtime/webhooks/EventGrid?functionName=<my-function-name> 。然后我收到以下错误消息:

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."}

据我所知,应用程序需要某种请求验证。我真的需要在每个 azure 函数中实现验证吗?或者我应该使用其他端点类型?

最佳答案

当您在事件网格中输入 Webhook 时,它会发出一个请求来验证您是否确实拥有该端点的权限。将函数连接到事件网格的最简单方法是从函数应用而不是事件网格边栏选项卡创建订阅。

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

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

enter image description here

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

// 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
}

还有一个选项 validate the link manually通过从验证消息中获取验证链接并在浏览器中导航至该链接。此方法主要适用于无法添加验证码的第三方服务。

关于Azure函数: How to add row to cloud table on blob creating?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com