【问题标题】:is it possible to customize the events that a blob within a storage account fires on blob creation?是否可以自定义存储帐户中的 blob 在创建 blob 时触发的事件?
【发布时间】:2019-10-08 00:13:40
【问题描述】:

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

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

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

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

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

默认架构如下所示:

{
    "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 azure-storage azure-blob-storage azure-eventgrid


    【解决方案1】:

    Azure 事件网格仅支持自定义和事件域主题的 CustomInputSchema。也就是说,AEG 内置事件源只能通过 EventGridSchema(默认模式)或 CloudEventV01Schema 进行分发。

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

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

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

    以下代码 sn-p 显示了 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 交付时,上述映射将应用于事件交付。

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 2019-11-16
      • 2023-01-10
      • 1970-01-01
      • 2019-11-25
      • 2018-01-19
      • 2020-07-18
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多