TIP
💡 Learn more : Azure storage account overview (opens new window).
# Access all files from an Azure Storage Blob Container
Azure Storage is described as a service that provides storages that is available, secure, durable, scalable, and redundant. Azure Storage consists of 1) Blob storage, 2) File Storage, and 3) Queue storage. In the past posts (opens new window), I've described how to create an Azure Storage account through the Portal and how to (opens new window) create an Azure Storage Blob Container through C#. I've even detailed how to upload and download a stream into an Azure Storage Blob](https://microsoft.github.io/AzureTipsAndTricks/blog/tip76.html) but I've always grabbed one file at a time. In this post, I'll show you how you can quickly access all the files of a given extension in a container instead of a single one.
# Download a Single File in an Azure Storage Blob Container
If you have an existing container and know the filename, then you can use this code (which was covered before) to grab a single file.
static void Main(string[] args)
{
BlobServiceClient storageAccount = new BlobServiceClient(CloudConfigurationManager.GetSetting("StorageConnection"));
BlobContainerClient container = storageAccount.GetBlobContainerClient("images-backup");
container.CreateIfNotExists(PublicAccessType.Blob);
BlockBlobClient blockBlob = container.GetBlockBlobClient("mikepic.png");
using (var fileStream = System.IO.File.OpenWrite(@"C:\Users\mbcrump\Downloads\mikepic-backup.png"))
{
blockBlob.DownloadTo(fileStream);
}
Console.ReadLine();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# Download all File Extensions in an Azure Storage Blob Container
If you have an existing container and want to pull down all the files for a specific type, then you can use this code.
BlobServiceClient storageAccount = new BlobServiceClient(CloudConfigurationManager.GetSetting("StorageConnection"));
BlobContainerClient container = storageAccount.GetBlobContainerClient("images-backup");
container.CreateIfNotExists(PublicAccessType.Blob);
var list = container.GetBlobs();
var blobs = list.Where(b => Path.GetExtension(b.Name).Equals(".png"));
foreach (var item in blobs)
{
string name = item.Name;
BlockBlobClient blockBlob = container.GetBlockBlobClient(name);
using (var fileStream = File.OpenWrite(@"C:\Users\mbcrump\Downloads\test\" + name))
{
blockBlob.DownloadTo(fileStream);
}
}
Console.ReadLine();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17