使用 c# 获取与 azure 存储 blob 中的元数据键和值匹配的 blob 列表

问题描述 投票:0回答:1

我在天蓝色存储 blob 中的 blob 容器中有不同的文件夹

我的结构为

容器名称/父文件夹/子文件夹1/Blob 容器名称/父文件夹/子文件夹2/Blob 容器名称/父文件夹/子文件夹3/Blob

在每个文件夹中,我都有元数据键为“Account”、其值为“Test1”的 blob

我正在尝试使用以下代码获取 blob 详细信息列表,无论基于元数据键和值的文件夹如何,但它不返回结果

我在下面的代码中做错了什么吗

private IOrderedEnumerable<CloudBlockBlob> GetListOfBlobs(string blobConnString)
 {

     var cloudStorageAccount = CloudStorageAccount.Parse(blobConnString);
     var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
     var blobContainer = cloudBlobClient.GetContainerReference("ContainerName");
     var blobList = blobContainer.ListBlobs("ParentFolder");
     var matchBlobs = blobList.OfType<CloudBlockBlob>().Where(r => r.Metadata.Any(r => r.Key == "Account" && r.Value == "Test1")).ToList().OrderBy(d => d.Properties.Created);
     return matchBlobs;
 }
c# azure-blob-storage
1个回答
0
投票

使用 c# 从 azure 存储 blob 获取与元数据键和值匹配的 blob 列表。

您可以使用以下代码,其中使用

Azure.Storage.Blobs
包列出与元数据键和值匹配的 blob。

在我的环境中相同的文件夹结构:

test/(container)
   foo/(parent folder)
     bar/with blobs(subfolder) 
     demo/with blobs(subfolder)
     test/with blobs(subfolder)

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace BlobMetadataExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string blobConnectionString = "xxx";
            string containerName = "test";
            string folderPath = "foo"; // Specify the folder path prefix
            string metadataKey = "Account"; 
            string metadataValue = "Test1"; /

            BlobContainerClient blobContainerClient = new BlobContainerClient(blobConnectionString, containerName);

            var blobs = await GetBlobsByMetadataAsync(blobContainerClient, folderPath, metadataKey, metadataValue);

            Console.WriteLine("Blobs with specified metadata:");
            foreach (var blob in blobs)
            {
                Console.WriteLine($"Blob Name: {blob.Name}, Created On: {blob.Properties.CreatedOn}");
            }
        }
        public static async Task<IOrderedEnumerable<BlobItem>> GetBlobsByMetadataAsync(BlobContainerClient blobContainerClient, string folderPath, string metadataKey, string metadataValue)
        {
            var matchingBlobs = new List<BlobItem>();
            await foreach (BlobItem blobItem in blobContainerClient.GetBlobsAsync(prefix: folderPath, traits: BlobTraits.Metadata))
            {
                if (blobItem.Metadata.TryGetValue(metadataKey, out var value) && value == metadataValue)
                {
                    matchingBlobs.Add(blobItem);
                }
            }

            return matchingBlobs.OrderBy(blob => blob.Properties.CreatedOn);
        }
    }
}

输出:

Blobs with specified metadata:
Blob Name: foo/bar/industry.csv, Created On: 06-11-xxx 07:49:24 +00:00
Blob Name: foo/bar/sample5000.xlsx, Created On: 06-11-xxxx 10:05:26 +00:00
Blob Name: foo/demo/cert_2.key, Created On: 13-11-x 03:5xxxx 5:56 +00:00
Blob Name: foo/test/Octkb2 24.md, Created On: 13-11-xxxx 03:56:19 +00:00

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.