Azure 问题列表 blob

Azure issues listing blobs

我正在尝试列出并从 azure blob 存储中获取容器中的 blob,以便能够下载文件,并通过我的 API 发送结果。我已按照 Microsoft 此处的说明进行操作:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list?tabs=dotnet

我的 blob 文件结构是 {Guid}/images/{Guid}.jpg,其中 {Guid} 是带有破折号的 Guid。

然而,这两种方法实际上都不起作用。我已经尝试过使用平面列表(考虑到我有分层嵌套,这将不起作用):

        var resultSegment = containerClient.GetBlobsAsync().AsPages();

        await foreach(var blobPage in resultSegment)
        {
            foreach(var blobItem in blobPage.Values)
            {

                using(var stream = new MemoryStream())
                {
                    var combinedName = Path.Combine("images/", blobItem.Name);
                    var blobClient = containerClient.GetBlobClient(combinedName);

                    await blobClient.DownloadToAsync(stream);
                    result.Add(
                    new AzureBlobResponse
                    {
                        Data = stream.ToArray(),
                        Filename = blobItem.Name
                    });
                }
            }
        }

仅仅抛出一个关于输入字符串的“System.FormatException”。然后我尝试将我的代码修改为:

private async Task NavigateBlobHierarchy(List<AzureBlobResponse> result, string? prefix)
    {
        var resultSegment = containerClient.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/")
            .AsPages();

        await foreach(var blobPage in resultSegment)
        {
            foreach(var blobItem in blobPage.Values)
            {
                if(blobItem.IsPrefix)
                {
                    await NavigateBlobHierarchy(result, blobItem.Prefix.Replace('/', ' ').Trim());
                } else
                {
                    using(var stream = new MemoryStream())
                    {
                        var combinedName = Path.Combine("images/", blobItem.Blob.Name);
                        var blobClient = containerClient.GetBlobClient(combinedName);

                        await blobClient.DownloadToAsync(stream);
                        result.Add(
                        new AzureBlobResponse
                        {
                            Data = stream.ToArray(),
                            Filename = blobItem.Blob.Name
                        });
                    }
                }
            }
        }
    }

我这样称呼它的地方:

        try
        {
            var listOfBlobs = new List<AzureBlobResponse>();
            await NavigateBlobHierarchy(listOfBlobs, "images");

            return listOfBlobs;
        } catch (Exception e)
        {
            throw;
        }

这只会陷入无限循环,因为它会挂在图像前缀上,而且它总是命中的所有内容都是图像。我到底做错了什么,这不应该是一个需要解决的复杂问题。在我的老地方的项目中使用旧的 API Azure blob 之前,我已经完成了它,从来没有这种奇怪的情况。

尝试使用此代码。我在我的系统中尝试过它能够列出 blob 中的所有文件并在本地下载它们。

BlobServiceClient blobServiceClient = new BlobServiceClient("Connection string");

            // Get the container client object
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");

            // 1st method to List all blobs in the container
            foreach (BlobItem blobItem in containerClient.GetBlobs())
            {
                Console.WriteLine("\t" + blobItem.Name);
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("ConnectionString");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test");

            //2nd method to List all the files
            var blobList = container.ListBlobs(useFlatBlobListing: true);
            foreach (var blob in blobList)
            {
                Console.WriteLine("\t" + blob.Uri);
                string name = ((CloudBlockBlob)blob).Name;
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                string path = (@"local path");
                string[] names = name.Split("/");
                string newPath = "";
                string fileName = "";
                for (int i = 0; i < names.Length; i++)
                {
                    if (i != (names.Length - 1))
                    {
                        path = path  +names[i]+"\";
                        newPath = newPath + "\" + names[i];
                    }
                    fileName = names[(names.Length - 1)];
                }
                string filePath = path  + fileName;
                if (Directory.Exists(path))
                {
                    blockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
                }
                //blockBlob.DownloadToFile(path, FileMode.OpenOrCreate);
                else
                {
                  
                    Directory.CreateDirectory(path);
                    string filePath1 = path +fileName;
                    blockBlob.DownloadToFile(filePath1, FileMode.OpenOrCreate);
                }

            }

输出

容器中的文件(在 Azure Blob 存储中)

列出的文件

本地文件保存

我有一个文件夹名称包含名为my learning inside images的文件,如图所示

与本地保存的文件结构相同