3

I have the following list of blobs:

  • VirtualDirectroy1/VirtualSubDirectory1/Blob1
  • VirtualDirectroy2/VirtualSubDirectory2/Blob2
  • VirtualDirectroy3/VirtualSubDirectory3/Blob3

I need to list Blob1, Blob2 and Blob3, so that when accessing the CloudBlockBlob.Name property it returns just Blob1, Blob2 or Blob 3 WITHOUT prefix of virtual directories.

How can I archive this?

Best Wishes, Oleg

3 Answers 3

3

If you are using the Azure storage .Net client library (I am using version 3.0.3 in which these methods/overloads are available), you could do something this:

var container = GetBlobContainer();
foreach (var blobItem in container.ListBlobs(useFlatBlobListing: true))
{
    Console.WriteLine(blob.Parent.Uri.MakeRelativeUri(blob.Uri));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Even though I list blobs using useFlatBlobListing=true, the CloudBlockBlob.Name property still returns a blob's name including virtual directories. However, the blob.Parent.Uri.MakeRelativeUri(blob.Uri) makes the trick that I need. Thanks for your help.
It seems that if a file is not in a virtual directory (i.e. directly in the container 'root') then this solution will write containerName/fileName
2

I found you can do this:

var storageAccountConnectionString = "...";
var storageAccount = CloudStorageAccount.Parse(storageAccountConnectionString);

var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference("containerName");
foreach (var blob in cloudBlobContainer.ListBlobs())
{
    Console.WriteLine(blob.Uri.Segments.Last());
}

1 Comment

You need to remove the trailing / ;)
0
class Program
{
    const string _conStr = "storage connection string";
    const string _container =  "container name";
    static void Main(string[] args)
    {
        var names = new Program().GetBlobNames();
        Console.ReadKey();
    }

    private List<string> GetBlobNames()
    {
        CloudStorageAccount acc = CloudStorageAccount.Parse(_conStr);
        CloudBlobClient blobClient = acc.CreateCloudBlobClient();
        CloudBlobContainer cntnr = blobClient.GetContainerReference(_container);
        List<IListBlobItem> blobList = cntnr.ListBlobs("").ToList();
        List<string> flatList = new List<string>();
        List<IListBlobItem> blobItems = new List<IListBlobItem>();
        foreach (IListBlobItem blobItem in blobList)
        {
            //If it is cloudblob directory
            if (blobItem.GetType() == typeof(CloudBlobDirectory))
            {
                CloudBlobDirectory dir = blobItem as CloudBlobDirectory;
                GetFilesInDirectory(dir, blobItems);
            }
        }
        return blobItems.Select(b => b.Parent.Uri.MakeRelativeUri(b.Uri).ToString()).ToList();            
    }


    private void GetFilesInDirectory(CloudBlobDirectory directory, List<IListBlobItem> fileList)
    {
        foreach (var blobItem in directory.ListBlobs())
        {
            if (blobItem.GetType() == typeof(CloudBlockBlob))
            {
                CloudBlockBlob blob = (CloudBlockBlob)blobItem;                    
                fileList.Add(blob);
            }
            else if (blobItem.GetType() == typeof(CloudPageBlob))
            {
                CloudPageBlob blob = (CloudPageBlob)blobItem;
                fileList.Add(blob);
            }
            else if (blobItem.GetType() == typeof(CloudBlobDirectory))
            {
                //Fetch recursively all the blobs
                CloudBlobDirectory blob = (CloudBlobDirectory)blobItem;
                GetFilesInDirectory(blob, fileList);
            }
        }
    }
}

1 Comment

how to get all files from specific container? including directory and subdirectory...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.