Blob Storage in Azure Cloud

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

Azure Blob storage is a service that stores unstructured data in the Cloud as objects/blobs. Blob storage can store any text or binary data, such as a document, media file, or application installer. Blob storage is also referred to as object storage.

Blob Storage

Azure Blob storage is a service for storing large amounts of unstructured object data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world or to store application data privately. Common uses of blob storage include:

  • Serving images or documents directly to a browser.
  • Storing files for distributed access.
  • Streaming video and audio.
  • Storing data for backup and restore disaster recovery, and archiving.
  • Storing data for analysis by an on-premises or Azure-hosted service.

This document illustrates how to create, access, and delete a container and blob in Azure through C#:

  • Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs. Note that the container name must be lowercase.
  • Storage Account: All access to Azure Storage is done through a storage account. This storage account can be a general-purpose storage account or a blob storage account which is specialized for storing objects/blobs.
  • Blob: A file of any type and size. Azure Storage offers three types of blobs: block blobs, page blobs, and append blobs.

First of all, we need to declare the following private members:

private CloudBlobClient _blobClient; private string _containerName;
private string _blobName;
private StorageCredentials _storageCredentials = new
StorageCredentials("your_login", "the_password_from_azure");
private CloudStorageAccount _storageAccount;
private CloudBlobContainer _container;
private CloudBlockBlob _blockBlob;

Note: Add the next namespaces to access Azure blob storage:

Using Microsoft.WindowsAzure.Storage;
Using Microsoft.WindowsAzure.Storage.Auth;
Using Microsoft.WindowsAzure.Storage.Blob;

Then, initialize a new instance of the class with the specified container and storage credentials.

public AzureBlobManager(string containerName,
      StorageCredentials storageCredentials) {
   _storageAccount = new
      CloudStorageAccount(storageCredentials, false);
   _blobClient = _storageAccount.CreateCloudBlobClient();
   _containerName = containerName;
   _container = _blobClient.GetContainerReference(_containerName);
}

Download Contents of Blob

The following program displays how to download blob contents to bytes.

public byte[] GetBlob(string containerName, string blobName)
{ CloudBlobContainer container =
   _blobClient.GetContainerReference(containerName);
   _blockBlob = container.GetBlockBlobReference(blobName);
   _blockBlob.FetchAttributes();

   long fileByteLength = _blockBlob.Properties.Length;
   byte[] fileContents = new byte[fileByteLength];
   _blockBlob.DownloadToByteArray(fileContents, 0);

   return fileContents;
}

Update or Create a Blob in Azure

public PutBlob(string containerName, string blobName,
      byte[] content) {

   CloudBlobContainer container =
      _blobClient.GetContainerReference(containerName);
      _blockBlob = container.GetBlockBlobReference(blobName);
   using (var stream = new MemoryStream(content, writable: false))
   { _blockBlob.UploadFromStream(stream); }
}

Create a Container in Azure Blob Storage

The next program displays how to create a container in blob storage.

public CreateContainer(string containerName) {
   CloudBlobContainer container =
      _blobClient.GetContainerReference(containerName);
   container.Create();
}

Delete a Container in Azure Blob Storage

This program displays how to delete a container in blob storage.

public DeleteContainer(string containerName) {
   CloudBlobContainer container =
      _blobClient.GetContainerReference(containerName);
   container.Delete();
}

How to Check Whether a Container Exists or Not in Azure Blob storage

The following program displays how to check whethe or notr a container exists.

public bool DoesContainerExist(string containerName) {
   IEnumerable<CloudBlobContainer>
      containers = _blobClient.ListContainers();
      returnValue = containers.Any(one =>
      one.Name == containerName); return returnValue;
}

How to Check Whether a Blob Exists or Not in Azure Blob Storage

The next program displays how to check whether a blob exists or not.

public bool DoesBlobExist(string containerName, string blobName) {

   CloudBlobContainer container =
      _blobClient.GetContainerReference(containerName);
      _blockBlob = container.GetBlockBlobReference(blobName);
      returnValue = _blockBlob.Exists() return returnValue;
}

How to Delete a Blob from a Container in Azure Blob Storage

This program displays how to delete a blob.

public DeleteBlob(string containerName, string blobName) {

   CloudBlobContainer container =
      _blobClient.GetContainerReference(containerName);
      _blockBlob = container.GetBlockBlobReference(blobName);
      _blockBlob.Delete();
}

About the Author

Subash Thota works as a Data Architect and specializes in Big Data, Cloud, Data Integration, and Data Analytics. Subash has written several papers in the field of Big Data, the Cloud, and Analytics.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read