In this article, we are going to learn about all the operations that we may need to use in order to manage files in Azure Blob Storage using the .NET framework. Here are the operations that we will explore:
- Upload: a .NET function to upload files to Azure blob storage
- CopyTo: a .NET function to copy file in Azure blob storage
- Move: a .NET function to move a file from one folder to another in Azure blob storage
- MoveFolder: a .NET function to move folder from one location to another in Azure blob storage
- Delete: a .NET function to delete file from Azure blob storage
- Exists: a .NET function to check if file exists in Azure blob storage
- FileSize: a .NET function to get the file size of a file stored in Azure blob storage
Before we dig into the code, let's first understand the key components of the Azure blob storage.
Azure Blob Storage
Azure Blob storage is a Microsoft cloud storage. Blob storage can store a massive amount of files as unstructured data. The unstructured data means not belong to any specific type, which means we can store text files, images, videos, or binary data. Blob storage is designed for:
- Serving images or documents directly to a browser.
- Storing files for distributed access.
- Streaming video and audio.
- Writing to log files.
- Storing data for backup and restore, disaster recovery, and archiving.
- Storing data for analysis by an on-premises or Azure-hosted service.
The key components to creating an Azure Blob storage resource:
- The storage account
- A container in the storage account
- A blob in a container
Storage Account
A Storage account gives a unique namespace in Azure for all the data we will store. Every item that we store in Azure storage has an address. The address is nothing but the unique name of our storage account name. The combination of the account name and the Azure Blob endpoint forms the base address for each object in our Storage account. For example, if our storage account is named as 'adventureworks' then the base address will be like 'https://adventureworks.blob.core.windows.net'.
Containers
The containers are like folders, they are used to organize a set of blobs, similar to a directory in a file system. So storage account can have an unlimited number of containers. Inside each container we can have an unlimited number of blobs.
Blobs
There are 3 types of blobs that can be stored in Azure Blob storage:
- Block Blobs: store text and binary data. Block blobs are made up of blocks of data that can be managed individually. Block blobs can store up to about 190.7 TiB.
- Append Blobs: are made up of blocks like block blobs, but are optimized for append operations. Append blobs are ideal for scenarios such as logging data from virtual machines.
- Page Blogs: store random access files up to 8 TiB in size. Page blobs store virtual hard drive (VHD) files and serve as disks for Azure virtual machines.
So far we learned that core components of Azure Blob storage. Let's learn how to manage our Azure Blob storage using .NET framework and C#.
First Step: Install NuGet Package: WindowsAzure.Storage
Install-Package WindowsAzure.Storage -Version 9.3.3
Second Step: Create a new class AzureBlobStorage and add the CRUD operations as per the following source code. Note that you need to store the Azure Storage Connection String and the Blob Storage Container Name in the web.config or the app.config. You can get these settings from Azure portal:
How to Remove Special Characters from Azure Blob Storage File
//This function is used to remove special characters from the file name
private static string ManipulateFileName(string fileName)
{
if ((fileName.StartsWith("~")))
fileName = fileName.Remove(0, 1);
if ((fileName.StartsWith("/")))
fileName = fileName.Remove(0, 1);
return fileName;
}
How to Upload file to Azure Blob Storage
//This function is used to upload a file to Azure Blob storage
public static bool Upload(string fileName, Stream fileStream)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
fileName = ManipulateFileName(fileName);
// Retrieve reference to a blob named "blockBlob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Create or overwrite the "blockBlob" blob with contents from stream.
blockBlob.UploadFromStream(fileStream);
// Set the content type
blockBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(fileName);
// Set the CacheControl property to expire in seconds
blockBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
blockBlob.SetProperties();
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
How to Copy file in Azure Blob Storage
//This function is used to copy a file to Azure Blob storage
public static bool CopyTo(string sourceFileName, string destinationFileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFileName = ManipulateFileName(sourceFileName);
destinationFileName = ManipulateFileName(destinationFileName);
CloudBlockBlob srcBlob = container.GetBlockBlobReference(sourceFileName);
CloudBlockBlob destBlob = container.GetBlockBlobReference(destinationFileName);
if (srcBlob.Exists())
{
using (MemoryStream memoryStream = new MemoryStream())
{
srcBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
// destBlob = container.GetBlockBlobReference(destBlob.Name)
destBlob.UploadFromStream(memoryStream);
}
// Set the content type
destBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(destBlob.Name);
// Set the CacheControl property to expire in seconds
destBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
destBlob.SetProperties();
}
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
How to Move file in Azure Blob Storage
//This function is used to move a file to Azure Blob storage
public static bool Move(string sourceFileName, string destinationFileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFileName = ManipulateFileName(sourceFileName);
destinationFileName = ManipulateFileName(destinationFileName);
CloudBlockBlob srcBlob = container.GetBlockBlobReference(sourceFileName);
CloudBlockBlob destBlob = container.GetBlockBlobReference(destinationFileName);
if (srcBlob.Exists())
{
// Create the destination blob
using (MemoryStream memoryStream = new MemoryStream())
{
srcBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
// destBlob = container.GetBlockBlobReference(destBlob.Name)
destBlob.UploadFromStream(memoryStream);
}
// Set the content type
destBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(destBlob.Name);
// Delete the source blob.
srcBlob.Delete();
// Set the CacheControl property to expire in seconds
destBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
destBlob.SetProperties();
}
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
How to Move Folder in Azure Blob Storage
//This function is used to move a folder to Azure Blob storage
public static bool MoveFolder(string sourceFolderPath, string destinationFolderPath)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFolderPath = ManipulateFileName(sourceFolderPath);
destinationFolderPath = ManipulateFileName(destinationFolderPath);
CloudBlobDirectory srcBlob = container.GetDirectoryReference(sourceFolderPath);
CloudBlobDirectory destBlob = container.GetDirectoryReference(destinationFolderPath);
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
How to Delete File in Azure Blob Storage
//This function is used to delete a file to Azure Blob storage
public static bool Delete(string fileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
// Retrieve reference to the blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Delete the blob.
blockBlob.Delete();
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
How to Check if File Exists in Azure Blob Storage
//This function is used to check if a file exists in Azure Blob storage
public static bool Exists(string fileName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
// Check if blob already exists
return container.GetBlockBlobReference(fileName).Exists();
}
How to Get File Size in Azure Blob Storage
//This function is used to get the size of a file stored in Azure Blob storage
public static long FileSize(string fileName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Check if blob already exists
if (blockBlob.Exists())
return blockBlob.Properties.Length;
return 0;
}
Here is the consolidated code for the entire class, it includes all the C# crud operations to manage your files in Azure Blob Storage:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Configuration;
using System.IO;
public class AzureBlobStorage
{
//This function is used to upload a file to Azure Blob storage
public static bool Upload(string fileName, Stream fileStream)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
fileName = ManipulateFileName(fileName);
// Retrieve reference to a blob named "blockBlob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Create or overwrite the "blockBlob" blob with contents from stream.
blockBlob.UploadFromStream(fileStream);
// Set the content type
blockBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(fileName);
// Set the CacheControl property to expire in seconds
blockBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
blockBlob.SetProperties();
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
//This function is used to copy a file to Azure Blob storage
public static bool CopyTo(string sourceFileName, string destinationFileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFileName = ManipulateFileName(sourceFileName);
destinationFileName = ManipulateFileName(destinationFileName);
CloudBlockBlob srcBlob = container.GetBlockBlobReference(sourceFileName);
CloudBlockBlob destBlob = container.GetBlockBlobReference(destinationFileName);
if (srcBlob.Exists())
{
using (MemoryStream memoryStream = new MemoryStream())
{
srcBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
// destBlob = container.GetBlockBlobReference(destBlob.Name)
destBlob.UploadFromStream(memoryStream);
}
// Set the content type
destBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(destBlob.Name);
// Set the CacheControl property to expire in seconds
destBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
destBlob.SetProperties();
}
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
//This function is used to move a file to Azure Blob storage
public static bool Move(string sourceFileName, string destinationFileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFileName = ManipulateFileName(sourceFileName);
destinationFileName = ManipulateFileName(destinationFileName);
CloudBlockBlob srcBlob = container.GetBlockBlobReference(sourceFileName);
CloudBlockBlob destBlob = container.GetBlockBlobReference(destinationFileName);
if (srcBlob.Exists())
{
// Create the destination blob
using (MemoryStream memoryStream = new MemoryStream())
{
srcBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
// destBlob = container.GetBlockBlobReference(destBlob.Name)
destBlob.UploadFromStream(memoryStream);
}
// Set the content type
destBlob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(destBlob.Name);
// Delete the source blob.
srcBlob.Delete();
// Set the CacheControl property to expire in seconds
destBlob.Properties.CacheControl = "public, max-age=" + ConfigurationManager.AppSettings["CloudStorageCacheMaxAge"];
// Update the blob's properties in the cloud
destBlob.SetProperties();
}
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
//This function is used to move a folder to Azure Blob storage
public static bool MoveFolder(string sourceFolderPath, string destinationFolderPath)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storAcc = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storAcc.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
BlobContainerPermissions permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
sourceFolderPath = ManipulateFileName(sourceFolderPath);
destinationFolderPath = ManipulateFileName(destinationFolderPath);
CloudBlobDirectory srcBlob = container.GetDirectoryReference(sourceFolderPath);
CloudBlobDirectory destBlob = container.GetDirectoryReference(destinationFolderPath);
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
//This function is used to delete a file to Azure Blob storage
public static bool Delete(string fileName)
{
try
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
// Retrieve reference to the blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Delete the blob.
blockBlob.Delete();
return true;
}
catch (Exception ex)
{
//Log the error
return false;
}
}
//This function is used to check if a file exists in Azure Blob storage
public static bool Exists(string fileName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
// Check if blob already exists
return container.GetBlockBlobReference(fileName).Exists();
}
//This function is used to get the size of a file stored in Azure Blob storage
public static long FileSize(string fileName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["BlobStorageContainerName"]);
fileName = ManipulateFileName(fileName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Check if blob already exists
if (blockBlob.Exists())
return blockBlob.Properties.Length;
return 0;
}
//This function is used to remove special characters from the file name
private static string ManipulateFileName(string fileName)
{
if ((fileName.StartsWith("~")))
fileName = fileName.Remove(0, 1);
if ((fileName.StartsWith("/")))
fileName = fileName.Remove(0, 1);
return fileName;
}
}