azure-functions - 事件网格触发 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 元数据。
以下代码片段显示了此实现示例:
运行.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 文件的两个属性,例如contentType和contentLength。
上述实现用于避免订阅者对具有意外值的 blob 属性 contentType 循环DeleteBlob,例如:abcd。
如果软删除的 blob 允许检索其属性和/或元数据,那就太好了。我在这里写了这个选项的反馈。
推荐阅读
- javascript - 在 acrobat pro dc 中的保存按钮上执行 javascript
- wordpress - 谷歌对我所有的 Laravel 网站说“前面的欺骗性网站”
- javascript - 删除项目时不会呈现 redux 存储
- jupyter-notebook - 使用 jupyterthemes 和目录修复 jupyter notebook 工具栏
- r - lapply + ggplot 无法填充我的箱线图。(箱线图中没有颜色填充)
- python - violinplot 上的观察次数不好
- c# - Stripe-Metadata 没有响应 (WebHook/.net-core)
- php - 如何让表格显示重复的标题和时间作为结果?
- javascript - 无法获取变量中的值
- linux - 在将 linux 内核编译为旧版本(2.6.11)时,我被卡住了