Toubi
Toubi

Reputation: 2509

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

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 my below code and guide:

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference(container);

var list = backupContainer.ListBlobs();

Upvotes: 44

Views: 112280

Answers (10)

helix-nebula
helix-nebula

Reputation: 296

Update:

Getting list of names of Azure blob files with Azure.Storage.Blobs v12 - Package

var storageConnectionString = "DefaultEndpointsProtocol=...........=core.windows.net";
var blobServiceClient = new BlobServiceClient(storageConnectionString);

//get container
var container = blobServiceClient.GetBlobContainerClient("container_name");

List<string> blobNames = new List<string>();

//Enumerating the blobs may make multiple requests to the service while fetching all the values
//Blobs are ordered lexicographically by name
//if you want metadata set BlobTraits - BlobTraits.Metadata
var blobs = container.GetBlobsAsync(BlobTraits.None, BlobStates.None);
await foreach (var blob in blobs)
{
    blobNames.Add(blob.Name);
}

There are more option and example you can find it here.

This is the link to the nuget package.

Upvotes: 8

Simran Kaur
Simran Kaur

Reputation: 277

We have to use ListBlobsSegmentedAsync() method, Then we can find out the blob by the following code :

public CloudBlockBlob GetLatestBlobByBlobNamePattern(CloudBlobContainer container, string blobNamePattern) 
        {
            var root = container.GetDirectoryReference(string.Empty);
            var blobsList = root.ListBlobsSegmentedAsync(null);
            blobsList.Wait();
            BlobResultSegment segment = blobsList.Result;
            List<IListBlobItem> list = new List<IListBlobItem>();
            list.AddRange(segment.Results);
            while (segment.ContinuationToken != null)
            {
                var blobs = container.ListBlobsSegmentedAsync(segment.ContinuationToken);
                blobs.Wait();
                segment = blobs.Result;
                list.AddRange(segment.Results);
            }
            var blob = list.Where(x => x.Uri.Segments.Last().Contains(blobNamePattern)).FirstOrDefault();
            return (CloudBlockBlob)blob;
        }

Upvotes: 0

Jonathan Allen
Jonathan Allen

Reputation: 70337

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;
    }

Upvotes: 6

Beena Gupta
Beena Gupta

Reputation: 178

  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

Upvotes: 2

Petra Stručić
Petra Stručić

Reputation: 131

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();

Upvotes: 12

Vignesh Raja
Vignesh Raja

Reputation: 610

We can get some additional info like Size, Modified date and Name.

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(YOUR_CON_STRING);

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference("CONTAINER");


var blobs = backupContainer.ListBlobs().OfType<CloudBlockBlob>().ToList();

foreach (var blob in blobs)
{
    string bName = blob.Name;
    long bSize = blob.Properties.Length;
    string bModifiedOn = blob.Properties.LastModified.ToString();        
}

Also you can download a specific file by Name.

 // Download file by Name
 string fileName = "Your_file_name";
 CloudBlockBlob blobFile = backupContainer.GetBlockBlobReference(fileName);
 blobFile.DownloadToFile(@"d:\"+ fileName, System.IO.FileMode.Create);

Upvotes: 15

Apollo
Apollo

Reputation: 2070

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();

Upvotes: 12

mikhail-t
mikhail-t

Reputation: 4221

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?

Upvotes: 16

feltocraig
feltocraig

Reputation: 739

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

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();

Upvotes: 36

Brendan Green
Brendan Green

Reputation: 11954

You can access the BlobProperties to get the name:

foreach (object o in list)
{
    BlobProperties bp = o as BlobProperties;
    if (bp != null)
    {
        BlobProperties p = _Container.GetBlobProperties(bp.Name);
        var name = p.Name; // get the name
    }
}

Upvotes: 2

Related Questions