首页 > 解决方案 > 事件网格触发 Azure 函数:如何访问已删除 blob 的元数据(用户定义的键、值对)?

问题描述

如何在事件网格触发 Azure 函数中获取已删除 blob 的元数据?下面是一个示例 C# 代码 -

[FunctionName("EventGridTriggerFunction")] 
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log) 
{ log.LogInformation(eventGridEvent.Data.ToString());
}

我可以从 EventGridEvent --> 数据对象属性中获取吗?有没有办法使用 blob 的元数据设置自定义事件架构?

参考链接 - https://docs.microsoft.com/en-us/azure/event-grid/event-schema-blob-storage

[{
  "topic": "/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/my-storage-account",
  "subject": "/blobServices/default/containers/test-container/blobs/new-file.txt",
  "eventType": "Microsoft.Storage.BlobCreated",
  "eventTime": "2017-06-26T18:41:00.9584103Z",
  "id": "831e1650-001e-001b-66ab-eeb76e069631",
  "***data***": {
    "api": "PutBlockList",
    "clientRequestId": "6d79dbfb-0e37-4fc4-981f-442c9ca65760",
    "requestId": "831e1650-001e-001b-66ab-eeb76e000000",
    "eTag": "\"0x8D4BCC2E4835CD0\"",
    "contentType": "text/plain",
    "contentLength": 524288,
    "blobType": "BlockBlob",
    "url": "https://my-storage-account.blob.core.windows.net/testcontainer/new-file.txt",
    "sequencer": "00000000000004420000000000028963",
    "storageDiagnostics": {
      "batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}]

标签: azure-functionsazure-blob-storageazure-webjobssdkazure-eventgrid

解决方案


您的解决方案没有优雅的解决方法。但是,启用存储帐户中的 blob软删除选项将能够在调用取消删除 blob 请求后获取EventGridTrigger订阅者中的 blob 元数据。

以下代码片段显示了此实现示例:

运行.csx:

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Linq;


public static async Task Run(JObject eventGridEvent, CloudBlockBlob blob, ILogger log)
{
    log.LogInformation($"{eventGridEvent}");

    if (eventGridEvent["data"]["contentType"].Value<string>() != "abcd" && eventGridEvent["data"]["api"].Value<string>() == "DeleteBlob")
    {
        await blob.UndeleteAsync();
        await blob.FetchAttributesAsync();           
    
        log.LogInformation($"\nMetadata: {string.Join(" | ", blob.Metadata.Select(i => $"{i.Key}={i.Value}"))}");
    
        // ...
    
        blob.Properties.ContentType = "abcd";
        await blob.SetPropertiesAsync();
        await blob.DeleteAsync();
    }
    else if (eventGridEvent["data"]["contentType"].Value<string>() != "abcd")
    {
        await blob.FetchAttributesAsync();           
    
        log.LogInformation($"\nMetadata: {string.Join(" | ", blob.Metadata.Select(i => $"{i.Key}={i.Value}"))}");

    }

    await Task.CompletedTask;
}

函数.json:

{
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent",
      "direction": "in"
    },
    {
      "type": "blob",
      "name": "blob",
      "path": "{data.url}",
      "connection": "rk2018ebstg_STORAGE",
      "direction": "in"
    }
  ],
  "disabled": false
}

请注意,存储帐户发出的事件消息中的数据对象仅包含 blob 文件的两个属性,例如contentTypecontentLength

上述实现用于避免订阅者对具有意外值的 blob 属性 contentType 循环DeleteBlob,例如:abcd

如果软删除的 blob 允许检索其属性和/或元数据,那就太好了。我在这里写了这个选项的反馈。


推荐阅读