Getting list of names of Azure blob files in a con

2019-01-26 03:16发布

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

6条回答
该账号已被封号
2楼-- · 2019-01-26 03:46

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();
查看更多
beautiful°
3楼-- · 2019-01-26 03:59

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

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-26 04:04

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?

查看更多
够拽才男人
5楼-- · 2019-01-26 04:05

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);
查看更多
forever°为你锁心
6楼-- · 2019-01-26 04:05

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
    }
}
查看更多
Rolldiameter
7楼-- · 2019-01-26 04:12

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();
查看更多
登录 后发表回答