簡體   English   中英

Azure 問題列出 Blob

[英]Azure issues listing blobs

我試圖列出並從 azure blob 存儲中獲取容器中的 blob,以便能夠下載文件,並通過我的 API 發送結果。 我已經按照微軟在這里的說明進行操作: 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;
        }

這只是以無限循環結束,因為它只是掛在了圖像前綴上,而且它總是命中的一切都是圖像。 我到底做錯了什么,這不應該是一個需要解決的復雜問題。 我在舊地方的項目中使用 Azure blob 的舊 API 之前已經這樣做了,從來沒有這種奇怪的情況。

嘗試使用此代碼。我在我的系統中嘗試過,能夠列出 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 的文件,如圖所示

在此處輸入圖片說明

與本地保存的文件結構相同

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM