查询Blob容器以检索文件

时间:2017-05-03 21:48:27

标签: c# azure-storage-blobs

我在Blob容器中有一些.png文件,我可以通过将文件名作为参数传递来检索文件:

var blob = container.GetBlockBlobReference(System.IO.Path.GetFileName(fileName));

但是我想查询Blob容器并列出它包含的所有文件。我该怎么做?

 public class FilesController : Controller
    {
        private CloudBlobContainer container;
        private CloudStorageAccount storageAccount;
        private CloudBlobClient blobClient;
        private CloudTableClient tableClient;
 public FilesController()
        {
            this.storageAccount = CloudStorageAccount.Parse(_dbConnection.ConnectionAzure);
            this.storageAccount.CreateCloudBlobClient();
            this.blobClient = storageAccount.CreateCloudBlobClient();
            this.container = blobClient.GetContainerReference("public/MyFolder");
            this.tableClient = storageAccount.CreateCloudTableClient();
        }

public ActionResult MyMethod(string fileName)
        {
           var blob = container.GetBlockBlobReference(System.IO.Path.GetFileName(fileName));
        }

1 个答案:

答案 0 :(得分:2)

这个怎么样:

var list = container.ListBlobs(useFlatBlobListing: true);
var listOfFileNames = new List<string>();

foreach (var blob in blobs) {
  var blobFileName = blob.Uri.Segments.Last();
  listOfFileNames.Add(blobFileName); 
}

return listOfFileNames;

来源:Getting list of names of Azure blob files in a container?