Getting list of names of Azure blob files in a container?

前端 未结 8 1630
醉酒成梦
醉酒成梦 2020-12-15 15:31

I need to list names of Azure Blob file names. Currently I m able to list all files with URL but I just need list of names. I want to avoid parsing names. Can you please see

相关标签:
8条回答
  • 2020-12-15 16:00
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
       CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare share = fileClient.GetShareReference(ConfigurationManager.AppSettings["ShareReference"]);
        if (share.Exists())
        {
              CloudFileDirectory rootDir = share.GetRootDirectoryReference();
              CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("paths");
              if (sampleDir.Exists())
              {
                    IEnumerable<IListFileItem> fileList = sampleDir.ListFilesAndDirectories();
                     //CloudFile file = sampleDir.GetFileReference(FileName + ext);
                     //return file;
               }
               return null;
          }
    

    From fileList one can get all the files from azure file

    0 讨论(0)
  • 2020-12-15 16:05

    The ListBlobs method doesn't appear to exist anymore. Here is an async verison.

        public static async Task<List<string>> ListBlobNamesAsync(CloudBlobContainer container)
        {
            var blobs = await ListBlobsAsync(container);
            return blobs.Cast<CloudBlockBlob>().Select(b => b.Name).ToList();
    
            //Alternate version
            //return blobs.Select(b => b.Uri.ToString()).Select(s => s.Substring(s.LastIndexOf('/') + 1)).ToList();
        }
    
        public static async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
        {
            BlobContinuationToken continuationToken = null; //start at the beginning
            var results = new List<IListBlobItem>();
            do
            {
                var response = await container.ListBlobsSegmentedAsync(continuationToken);
                continuationToken = response.ContinuationToken;
                results.AddRange(response.Results);
            }
    
            while (continuationToken != null); //when this is null again, we've reached the end
            return results;
        }
    
    0 讨论(0)
  • 2020-12-15 16:06

    This works with WindowsAzure.Storage 9.3.3.

    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
    
    var blobResultSegment = await cloudBlobContainer.ListBlobsSegmentedAsync(continuationToken);
    var blobs = blobResultSegment.Results.Select(i => i.Uri.Segments.Last()).ToList();
    
    0 讨论(0)
  • 2020-12-15 16:08

    Full answer with details.

            // Parse the connection string and return a reference to the storage account.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobConnectionString"));
    
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference("container_name");
    
            // Retrieve reference to a blob named "test.csv"
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("BlobName.tex");
    
            //Gets List of Blobs
            var list = container.ListBlobs();
            List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
    
    0 讨论(0)
  • 2020-12-15 16:09

    If you're using Windows Azure Storage 4.3.0, try this code.

    List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
    
    0 讨论(0)
  • 2020-12-15 16:15

    Here is one more way to get this done:

    CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);
    
    var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
    var backupContainer = backupBlobClient.GetContainerReference(container);
    
    // useFlatBlobListing is true to ensure loading all files in
    // virtual blob sub-folders as a plain list
    var list = backupContainer.ListBlobs(useFlatBlobListing: true);
    var listOfFileNames = new List<string>();
    
    foreach (var blob in blobs) {
      var blobFileName = blob.Uri.Segments.Last();
      listOfFileNames.Add(blobFileName); 
    }
    
    return listOfFileNames;
    

    Source: How to load list of Azure blob files recursively?

    0 讨论(0)
提交回复
热议问题