事件网格触发器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"
}]

解决方法

您的解决方案没有完美的解决方法。但是,存储帐户中的启用Blob的软删除选项将使您可以在调用取消删除的Blob请求后在 EventGridTrigger 订阅服务器中获取Blob元数据。

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

run.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;
}

function.json:

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

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

上述实现用于避免订户使用具有意外值的blob属性contentType来循环 DeleteBlob 。例如: abcd

如果软删除的Blob允许检索其属性和/或元数据,那将很好。我已经为此选项here写了反馈。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...