Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从BlockBlob元数据中获取以特定键字符串开头的所有值_C#_Azure Storage Blobs_Azure Blob Storage - Fatal编程技术网

C# 从BlockBlob元数据中获取以特定键字符串开头的所有值

C# 从BlockBlob元数据中获取以特定键字符串开头的所有值,c#,azure-storage-blobs,azure-blob-storage,C#,Azure Storage Blobs,Azure Blob Storage,我有一个具有多个元数据属性的BlockBlob图像。某些元数据属性是唯一的。其他的则以密钥标识符(在本例中为“标记”)开始 如何获取以特定字符串开头的元数据属性的所有值 下面是BlockBlob图像的示例: 有多个以索引0开头的“标记”元素 以下是手动执行此操作的方法,但您需要了解存在的每个索引: CloudBlobContainer container = await GetCloudBlobClientAsync(); CloudBlobDirectory directory = cont

我有一个具有多个元数据属性的
BlockBlob
图像。某些元数据属性是唯一的。其他的则以密钥标识符(在本例中为“标记”)开始

如何获取以特定字符串开头的元数据属性的所有值

下面是
BlockBlob
图像的示例:

有多个以索引0开头的“标记”元素

以下是手动执行此操作的方法,但您需要了解存在的每个索引:

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);

// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();

    // This is what I'm trying to do..
    var tagArray = [ blockBlob.Metadata["Tag0"], blockBlob.Metadata["Tag1"], ... ]
    // Returns ["outdoor", "nature", "man" ...]

    // Bonus if it included the key names as well..
    var tagArrayWithKeys = [ "Tag0": blockBlob.Metadata["Tag0"], ... ];
    // Returns [ "Tag0": "outdoor", "Tag1": "nature", "Tag2": "man", ...]
}

您将如何动态地做到这一点?

正如@Gaurav所说,如果您想要获取一个blob的所有元数据,您可以执行一个循环来获取它们。比如说

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);



// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();
    foreach(var r in blockBlob.Metadata){
     Console.WriteLine("Key: " + r.Key + " value: " + r.Value);

}

}
此外,如果您想通过键获取元数据过滤器,请参考以下代码

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);



// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();
    var result = blockBlob.Metadata.where(k => k.Key.StartsWith("Tag"))
    foreach(var r in result){
     Console.WriteLine("Key: " + r.Key + " value: " + r.Value);

}
}

CloudBlockBlob.Metadata是类型为
IDictionary
的对象。难道你不能简单地列举一下,然后从每个条目中提取键和值吗?@gauravmantri如果没有比“内容”更多的唯一标识符,那么这就是一种方法。我想列举一个
FromPattern
StartsWith
包含的