Accessing Azure Storage Account Blobs from C# Applications
In a previous post we went through accessing Azure File Shares with C#, today we will do the same with Azure Blob storage.
Azure Storage Blobs client library for .NET – Version 12.6.0
https://docs.microsoft.com/en-us/dotnet/api/overview/azure/storage.blobs-readme
Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data.
Installing the Azure Storage Blobs Client Library
In order to access the Azure Storage Blobs we have to use another API resp. assembly as for file share
As of today we need the Azure Storage Blobs client library for .NET – Version 12.7.0.
Therefore we can add the Azure.Storage.Blobs package from NuGet
From PowerShell
dotnet add package Azure.Storage.Blobs
I will use the same class as in my previous post Azure.cs, which will contain our code to access the Azure File Shares and now also the Azure Blob storage.
Inside our class Azure.cs, we first add a new using directive for the new Azure.Storage.Blobs reference, so that we do not to have write the whole namespaces in front of our commands.
using Azure.Storage.Blobs;
Upload Files to Blob Storage
Now first we create a new public method to upload a file into a blob storage. The method UploadToAzureBlob() needs three string parameters.
- sContainer will reference the name of the container from our Blob storage
- sBlobname is the name the Blob should be named after upload
- sPath is the local path + filename from the file we want to upload
The Connection String is requested from the App.config file as described in my last post about Azure File Shares.
As you can see, after referencing a container, you can create one with the container.Create() method, in my case the container was still created directly in the Azure Portal, so I commented this line.
Also you can decide to overwrite the file if already exists or not at the method blob.Upload().
We will use the BlobClient Class in this example, the Azure Storage Blobs client library is including several classes to manipulate Azure Blob Storage. A further class is the BlockBlobClient Class, you will see below another example using this class to upload a file.
// Using the BlobClient Class reside in the Azure.Storage.Blobs namespace public void UploadToAuzureBlob(string sContainer, string sBlobname, string sPath) { string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container, blobname and path string containerName = sContainer; string blobName = sBlobname; string filePath = sPath; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // container.Create(); // Get a reference to a blob named "sample.xlsx" in a container named "braintesting" BlobClient blob = container.GetBlobClient(blobName); // Upload local file bool overwrite = true; blob.Upload(filePath, overwrite); }
Another shorter alternative to upload a file is to use directly the BlobContainerClient Class as follows.
public void UploadToAuzureBlob(string sContainer, string sBlobname, string sPath) { string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container, blobname and path string containerName = sContainer; string blobName = sBlobname; string filePath = sPath; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // container.Create(); // Upload the file container.UploadBlob(sBlobname, File.OpenRead(sPath)); }
Finally we reference the Azure.cs class in the Program.cs class and trigger the UploadToAzureBlob(string sContainer, string sBlobname, string sPath) method in Azure.cs.
class Program { static void Main(string[] args) { Azure az = new Azure(); az.UploadToAuzureBlob("braintesting", "sample.xlsx", @"C:Tempattachmentssample.xlsx"); } }
Upload Files to a Subdirectory (Virtual Directory resp Blob Hierarchy)
As in contrast to Azure File Shares, you do not have real directories in Azure Storage Blobs but can use instead Virtual Directories.
Azure Storage Blob only supports a 2-level hierarchy.
A Virtual Directory is simply a prefix by using forward slashes in the name as a delimiter that you apply to a file (blob) name in order to provide a Blob hierarchy.
In order to upload a file/blob in a virtual directory, you only have to prefix the filename/blob with the virtual folder structure using forward slashes as delimiter as mentioned above.
One example, if you want to upload the sample.xlsx to a virtual directory like braintesting/Administration/A1/B1, the filename/blob string should be
braintesting/Administration/A1/B1/sample.xlsx
That’s it! In our case we only need to change the string sBlobname parameter at calling the UpoadToAzureBlob() method from only the filename into our virtual directory path prefix + filename.
// Azure az = new Azure(); az.UploadToAuzureBlob("braintesting", "braintesting/Administration/A1/B1/sample.xlsx", @"D:Tempsample.xlsx");
Using the BlockBlobClient Class
As already mentioned above, to manipulate the Azure Storage resources and blob containers, you can also use the the BlockBlobClient Class instead the BlobClient Class. The following example will also upload a file to the Blob Storage.
Therefore we will add a new using Directive as this class is homed in a sub namespace of Azure.Storage.Blobs.Specialized.
using Azure.Storage.Blobs.Specialized;
// Using the BlockBlobClient Class reside in the Azure.Storage.Blobs.Specialized namespace public void UploadToAuzureBlob(string sContainer, string sBlobname, string sFileName, string sPath) { string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container, blobname and path string containerName = sContainer; string blobName = sBlobname; string fileName = sFileName; string filePath = sPath; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // container.Create(); // Get a reference to the Blob BlockBlobClient blockBlob = container.GetBlockBlobClient(blobName); using (var fileStream = System.IO.File.OpenRead(filePath + fileName)) { blockBlob.Upload(fileStream); } }
The UploadToAzureBlob() method we will also call from the Main() method in the Program.cs class.
static void Main(string[] args) { Azure az = new Azure(); az.UploadToAuzureBlob("braintesting", "braintesting/Administration/A1/B1/sample.xlsx","sample.xlsx", @"D:Temp"); }
Download a Blob
The first example will use the BlobClient Class, we use in this example the code from our first example above to upload a file.
We only need to change the last method from blob.Upload() to blob.DownloadTo() as follows.
public void DownloadFromAzureBlob(string sContainer, string sBlobname, string sPath) { string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container, blobname string containerName = sContainer; string blobName = sBlobname; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // container.Create(); // Get a reference to a blob named "sample.xlsx" in a container named "braintesting" BlobClient blob = container.GetBlobClient(blobName); // Download the blob blob.DownloadTo(sPath + sBlobname); }
The DownloadFromAzureBlob() method we will call as usual from our Main() method in the Program.cs class again.
static void Main(string[] args) { Azure az = new Azure(); // Parameters are: <name of container>, <name of blob>, <path + name to download to the local system> az.DownloadFromAzureBlob("braintesting", "sample.xlsx", @"D:TempDownloads"); }
The second example will use the BlockBlobClient Class.
// BlockBlobClient class public void DownloadFromAzureBlob(string sContainer, string sBlobname, string sFileName, string sPath) { string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container, blobname and path string containerName = sContainer; string blobName = sBlobname; string fileName = sFileName; string filePath = sPath; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // container.Create(); // Get a reference to the Blob BlockBlobClient blockBlob = container.GetBlockBlobClient(blobName); // Download the blob blockBlob.DownloadTo(sPath + sFileName); }
The DownloadFromAzureBlob() method we will call as usual from our Main() method in the Program.cs class again.
static void Main(string[] args) { Azure az = new Azure(); // Parameter are: <name of container>, <name of blob including prefix virtual directory hierarchy>, <blob name>, <local path to download> az.DownloadFromAzureBlob("braintesting", "braintesting/Administration/A1/B1/sample.xlsx", "sample.xlsx", @"D:TempDownloads"); }
Traverse/Enumerating Blobs
If you want to check and list what blobs in the container are stored, you can traverse your container as follow.
In this example I will use a generic List<T> to store the enumerated blobs in it.
public List<string> GetBlobs(string sContainer) { // reference a new string list named folders which will include our blobs from our container braintesting in our Azure Storage Account var folders = new List<string>(); string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); // Name of the Container string containerName = sContainer; // Get a reference to a container named "braintesting" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // Enumerate all blobs in the container foreach (BlobItem blob in container.GetBlobs()) { folders.Add(blob.Name); } return folders; } }
We call the method GetBlobs() as follows inside the Program.cs class from the Main() method which is calling the TraverseBlob() method as follows.
class Program { static void Main(string[] args) { TraverseBlob(); } static void TraverseBlob() { Azure az = new Azure(); var folders = az.GetBlobs("braintesting"); folders.ForEach(delegate (string blob) { Console.WriteLine("Blob Name: " + blob); }); Console.ReadLine(); } }
Links
Azure Storage Blobs client library for .NET – Version 12.6.0
https://docs.microsoft.com/en-us/dotnet/api/overview/azure/storage.blobs-readme
Quickstart: Azure Blob storage client library v12 for .NET
https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet
BlobClient Class
https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobclient
BlockBlobClient Class
https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blockblobclient
STRUCTURED AND UNSTRUCTURED DATA: WHAT IS IT?
https://sherpasoftware.com/blog/structured-and-unstructured-data-what-is-it/