如何从Azure函数中的事件网格触发器获取blob的元数据



我的要求是从EventGridTrigger上的blob获取元数据。这个问题似乎很简单,但我在网上找不到例子。下面是我的Azure Funtion。我需要获取blob的元数据。获得它的最佳方式是什么。

public static void Run([EventGridTrigger] EventGridEvent eventGridEvent,
[Blob("{data.url}", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream input,
ExecutionContext context,
ILogger log)
{
// other lines of code
IDictionary<string, string> metadata = get_metadata_of_the_blob;
// other lines of code
}

我需要以某种方式在函数代码中获取元数据,位置为:get_Metadata_of_the_blob

尝试以下操作:

public static async Task Run(JObject eventGridEvent, CloudBlockBlob blob, ILogger log)
{
// ...
await blob.FetchAttributesAsync();           
log.LogInformation($"nMetadata: {string.Join(" | ", blob.Metadata.Select(i => $"{i.Key}={i.Value}"))}");
// ...
}

和函数.json:

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

首先需要一个博客实例,然后可以获得博客元数据。

try
{
BlobClient blob = new ....
// Get the blob's properties and metadata.
BlobProperties properties = await blob.GetPropertiesAsync();
Console.WriteLine("Blob metadata:");
// Enumerate the blob's metadata.
foreach (var metadataItem in properties.Metadata)
{
Console.WriteLine($"tKey: {metadataItem.Key}");
Console.WriteLine($"tValue: {metadataItem.Value}");
}
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
}

相关内容

最新更新