Getting list of names of Azure blob files in a con

2019-01-26 03:06发布

问题:

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

回答1:

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

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


回答2:

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?



回答3:

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


回答4:

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


回答5:

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


回答6:

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