How to get all files from a directory in Azure BLOB

Saravana Kumar picture Saravana Kumar · Feb 9, 2018 · Viewed 8.1k times · Source

We are storing the application logs in Azure BLOB storage. We are currently downloading the files using the complete URI to the file. Every time when the settings of Azure BLOB is changed, the file name also gets changed. So we are getting error as file not exists.

Suggest a way to get the files using file extension from a directory without having file name in UI.

Answer

Martin Brandl picture Martin Brandl · Feb 9, 2018

Use the ListBlobs() method to retrieve all blobs for a specific container and then you could use the static .NET Path.GetExtension() method to retrieve the file extension so you can filter them. Example:

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

var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(container);

var list = container.ListBlobs();
var blobs = list.OfType<CloudBlockBlob>()
    .Where(b => Path.GetExtension(b.Name).Equals("yourextension"))