azure - 是否可以自定义存储帐户中的 blob 在创建 blob 时触发的事件?

标签 azure azure-storage azure-blob-storage azure-eventgrid

是否可以更改在 blobcreated 上触发的默认事件?

存储帐户能够在删除/创建 blob 时触发事件:

enter image description here

如果您添加新的事件订阅,您可以在以下三个之间进行选择:

enter image description here

我希望能够使用自定义输入架构。但是,没有关于如何使用它的文档。

我们如何自定义自定义输入架构?

默认架构如下所示:

{
    "topic": "/subscriptions/xxxxxxxxxxx/resourceGroups/myresourcegroup/providers/Microsoft.Storage/storageAccounts/mystoraccount",
    "subject": "/blobServices/default/containers/xmlinput/blobs/myj.json",
    "eventType": "Microsoft.Storage.BlobCreated",
    "eventTime": "2019-05-20T18:58:28.7390111Z",
    "id": "xxxxxxxxxxxxxxxx",
    "data": {
        "api": "PutBlockList",
        "clientRequestId": "xxxxxxxxxxxxxxxx",
        "requestId": "xxxxxxxxxxxxxxxx",
        "eTag": "0x8D6DD55254EBE75",
        "contentType": "application/json",  
        "contentLength": 874636,
        "blobType": "BlockBlob",
        "url": "https://mystoraccount.blob.core.windows.net/xmlinput/myj.json",
        "sequencer": "00000000000000000000000000005FAC0000000000614963",
        "storageDiagnostics": {
            "batchId": "xxxxxxxxxxxxxxxx"
        }
    },
    "dataVersion": "",
    "metadataVersion": "1"
}

我只想返回文件名,在本例中它是 subject 的子字符串,myj.json

我们如何自定义正在触发的事件?

期望的结果:

{
  "filename": "myj.json"
}

最佳答案

Azure 事件网格仅支持自定义和事件域主题的CustomInputSchema。换句话说,AEG 内置事件源只能使用 EventGridSchema(默认架构)或 CloudEventV01Schema 进行分发。

对于您的解决方案,当您的使用者需要使用自定义架构订阅 AEG 事件时,您需要使用 CustomInputSchema 将事件链接到自定义主题。CustomInputSchema。以下屏幕片段显示了这个概念:

enter image description here

对于主题链接(集成器),可以使用无服务器 Azure Function 或 Api 管理。在我的测试中(如上图所示)使用了EventGridTrigger函数。

集成商有责任使用自定义架构触发 AEG 自定义主题端点。

以下代码片段显示了 EventGridTrigger 集成器的示例:

#r "Newtonsoft.Json"

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

static HttpClient client = new HttpClient() { BaseAddress = new Uri (Environment.GetEnvironmentVariable("CustomTopicEndpointEventGrid")) };

public static async Task Run(JObject eventGridEvent, ILogger log)
{
    log.LogInformation(eventGridEvent.ToString());

    string url = $"{eventGridEvent["data"]?["url"]?.Value<string>()}";
    if(!string.IsNullOrEmpty(url))
    {
        // Fire event
        var response = await client.PostAsJsonAsync("", new[] { new { filename = url.Substring(url.LastIndexOf('/') + 1) } });
        log.LogInformation(response.ToString());
    }

    await Task.CompletedTask;
}

请注意,CustomInputSchema 仍处于预览状态,因此要使用自定义输入架构创建自定义主题,请遵循文档 here 。另外,还可以使用REST API,查看更多详情here .

以下是我使用 REST Api 通过 CustomInputSchema 创建自定义主题的负载示例:

    {
      "location": "westus",
      "tags": {
        "tag1": "abcd",
        "tag2": "ABCD"
      },
      "properties": {
        "inputSchema": "CustomEventSchema",
        "inputSchemaMapping": {
          "properties": {
            "id": {
              "sourceField": null
              },
            "topic": {
              "sourceField": null
              },
            "eventTime": {
              "sourceField": null
              },
            "eventType": {
              "sourceField": "myEventType",
              "defaultValue": "BlobCreated"
              },
            "subject": {
              "sourceField": "mySubject",
              "defaultValue": "/containers/xmlinput/blobs"
              },
            "dataVersion": {
              "sourceField": null,
              "defaultValue": "1.0"
              }
            },
         "inputSchemaMappingType": "Json"
        }
     }
  }

一旦您拥有带有 CustomInputSchema 的自定义主题,输出交付架构后面将跟随输入架构。在这种情况下,当您对此自定义主题的订阅将通过 EventGridSchema 进行传递时,上述映射将应用于事件传递。

关于azure - 是否可以自定义存储帐户中的 blob 在创建 blob 时触发的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241022/

相关文章:

azure - 如何与第三方共享 Azure Function 日志

Azure 表分区键自动设置?

node.js - Nodejs 和 Jimp 缩略图生成

azure - 部署azure函数应用程序时出现ARM模板错误

azure - 如何在运行时更新 Windows Azure 辅助角色

sql - Synapse/ADF - 如果预复制脚本中的动态配置列为 True,如何截断表

azure - 找不到类型 Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest

performance - Azure 存储模拟器性能问题

azure - 在 Azure Blob 存储中托管 ClickOnce 并使用自定义域

azure - 是否可以在没有公共(public) CNAME 记录的情况下为 Azure Blob 存储静态网站添加自定义域?