Azure functions 事件网格触发Azure函数:如何访问已删除Blob的元数据(用户定义的密钥、值对)?

Azure functions 事件网格触发Azure函数:如何访问已删除Blob的元数据(用户定义的密钥、值对)?,azure-functions,azure-blob-storage,azure-webjobssdk,azure-eventgrid,Azure Functions,Azure Blob Storage,Azure Webjobssdk,Azure Eventgrid,如何在事件网格触发器Azure函数中获取已删除blob的元数据?下面是一个C#代码示例- 我可以从EventGridEvent-->数据对象属性获取数据吗?有没有办法用blob的元数据设置自定义事件模式 转介链接- 您的解决方案没有优雅的变通方法。但是,在调用取消删除blob请求后,存储帐户中的启用blob软删除选项将启用EventGridTrigger订阅服务器中的blob元数据 以下代码段显示了此实现示例: run.csx: #r "Newtonsoft.Json" #r

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

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

转介链接-


您的解决方案没有优雅的变通方法。但是,在调用取消删除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;
}
请注意,存储帐户发出的事件消息中的数据对象仅包括blob文件的两个属性,如contentType和contentLength

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


如果软删除blob允许检索其属性和/或元数据,那就太好了。我已经为此选项编写了反馈。

blob文件的用户元数据不是事件消息中数据对象的一部分。只有当blob文件存在时,你才能得到它们。谢谢你,Roman!是的,你说得对。有别的办法吗?基本上我想显示整个审计跟踪信息。blob/文件的(上载/删除/修改)。通过RESTAPI(PutBlob)调用,我正在设置BLOB的元数据以及用户详细信息。如果您能帮助我,我将不胜感激。您的解决方案没有优雅的变通方法。但是,启用blob的软删除选项将允许在调用undelete blob请求后在eventgridtrigger订阅服务器中获取blob元数据。确定,但undelete blob请求也将恢复内容。我可以通过创建自定义事件数据/模式来实现吗?如果是,则如何?同样,您不能在blob存储帐户发出的事件消息中自定义数据对象。您的订户将调用undelete blob,然后获取blob元数据,检查元数据,设置元数据并再次删除此blob。这是从软删除blob中检索元数据的非优雅方式。从标记为已删除的不可见blob中获取元数据会很好,但目前不支持此功能。
[{
  "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"
}]
#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;
}
{
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent",
      "direction": "in"
    },
    {
      "type": "blob",
      "name": "blob",
      "path": "{data.url}",
      "connection": "rk2018ebstg_STORAGE",
      "direction": "in"
    }
  ],
  "disabled": false
}