我如何获取所有Blob,而不仅仅是获取根目录中的Blob

时间:2019-02-05 16:17:41

标签: c# asp.net-mvc azure-storage-blobs

我如何从天蓝色存储容器中变成所有斑点,而不仅仅是根中的斑点?

private CloudBlobContainer GetCloudBlobContainer()
        {

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=*;AccountKey=*;EndpointSuffix=core.windows.net");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test123");

            return container;
        }

        public ActionResult ListBlobs()
        {
            CloudBlobContainer container = GetCloudBlobContainer();
            List<string> blobs = new List<string>();
            BlobResultSegment resultSegment = container.ListBlobsSegmentedAsync(null).Result;
            foreach (IListBlobItem item in resultSegment.Results)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    blobs.Add(blob.Name);
                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob blob = (CloudPageBlob)item;
                    blobs.Add(blob.Name);
                }
                //Directory
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory dir = (CloudBlobDirectory)item;
                    blobs.Add(dir.Uri.ToString());
                }
            }

            return View(blobs);
        }

该代码仅返回根目录上的blob。但我也想在目录中添加blob。

谢谢

1 个答案:

答案 0 :(得分:0)

在调用useFlatBlobListing方法时,请指定ListBlobsSegmentedAsync为true。