How to work with Azure blob storage

How to work with Azure blob storage

Azure Blob Storage is a massively scalable, object storage service from Microsoft’s cloud computing platform, Azure. It is optimized for storing large amounts of unstructured data — data that doesn’t conform to a particular data model or definition such as text or binary data.

In this article, we will explore the key concepts of Azure Blob Storage, its potential use-cases, and how to interact with it.

What is Azure Blob Storage?

Azure Blob Storage allows you to store and retrieve large amounts of data in a cost-effective manner, and it’s accessible from anywhere in the world via HTTP or HTTPS.

Blob Storage can manage thousands of simultaneous uploads, massive amounts of video data, continually growing log files, and can be reached from any part of the world. These characteristics make Azure Blob Storage a perfect fit for data archiving, backup, and disaster recovery, as well as large-scale, cloud-native application scenarios.

Types of Blobs

Azure Blob Storage offers three types of resources:

  1. Blob storage accounts — These serve as top-level containers for organizing your blobs.
  2. Containers — These are similar to directories in a file system. You can group similar blobs together in a container.
  3. Blobs — These are the individual objects or files that are stored in a container.

Blob Storage supports three types of blobs:

  1. Block blobs — Ideal for storing text and binary data, such as documents, media files, etc. Block blobs are made up of blocks, each of which is identified by a block ID.
  2. Append blobs — Similar to block blobs, but optimized for append operations, such as logging data from virtual machines.
  3. Page blobs — Used for read/write operations and storing random-access files up to 8 TB in size. They are ideal for frequently modified data, such as a virtual hard drive in Azure Virtual Machines.

Getting Started with Azure Blob Storage

To use Azure Blob Storage, you’ll first need to create a storage account, then a container within that account, and finally, you can upload blobs to that container.

Here’s an example of how to create a new blob and upload data to it using Azure’s .NET SDK:

string storageConnectionString \= "";
string containerName \= "";
string blobName \= "";

// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient \= new BlobServiceClient(storageConnectionString);

// Create the container and return a container client object
BlobContainerClient containerClient \= blobServiceClient.GetBlobContainerClient(containerName);

// Create a new blob in the container
BlobClient blobClient \= containerClient.GetBlobClient(blobName);

// Upload data to the blob
string blobContent \= "This is a blob content";
using MemoryStream memoryStream \= new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
await blobClient.UploadAsync(memoryStream, overwrite: true);

This code first establishes a connection to your Blob Storage account, then it creates a container client that you can use to interact with the specified container. Next, it obtains a blob client, and finally, it uploads data to the blob from a memory stream.

Getting blob information in Azure Blob Storage can be achieved using the BlobClient.GetPropertiesAsync method. This method retrieves properties and metadata for the blob.

Here’s a C# example showing how you can get blob information:

string storageConnectionString = "";
string containerName = "";
string blobName = "";

// Create a BlobServiceClient object
BlobServiceClient blobServiceClient = new BlobServiceClient(storageConnectionString);

// Get the blob container client
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

// Get the blob client
BlobClient blobClient = containerClient.GetBlobClient(blobName);

// Get blob properties
BlobProperties properties = await blobClient.GetPropertiesAsync();

// Print blob properties
Console.WriteLine($"Blob Name: {blobClient.Name}");
Console.WriteLine($"Blob Size: {properties.Value.ContentLength} bytes");
Console.WriteLine($"Blob Content Type: {properties.Value.ContentType}");
Console.WriteLine($"Blob Last Modified: {properties.Value.LastModified}");

If you want to loop through all blobs in an Azure Storage container, you can use the BlobContainerClient's GetBlobsAsync function. This function returns an async enumerable that can be iterated over using the await foreach construct in C#.

Here’s an example of how to list all blobs in a container:

string storageConnectionString = "";
string containerName = "";

// Create a BlobServiceClient object
BlobServiceClient blobServiceClient = new BlobServiceClient(storageConnectionString);

// Get the container client for the specified container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

// Call the GetBlobsAsync function and loop over its results
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
Console.WriteLine(blobItem.Name);
}

Moving a blob from one storage account to another can be achieved in two steps: copying the blob to the destination, then deleting it from the source. The Azure SDK provides functions for both.

Here’s a C# example showing how you can move a blob:

string sourceConnectionString \= "";
string sourceContainerName \= "";
string sourceBlobName \= "";

string destConnectionString \= "";
string destContainerName \= "";

// Create a BlobServiceClient for the source storage account
BlobServiceClient sourceBlobServiceClient \= new BlobServiceClient(sourceConnectionString);

// Get the source blob client
BlobClient sourceBlobClient \= sourceBlobServiceClient.GetBlobContainerClient(sourceContainerName).GetBlobClient(sourceBlobName);

// Create a BlobServiceClient for the destination storage account
BlobServiceClient destBlobServiceClient \= new BlobServiceClient(destConnectionString);

// Get the destination blob container client
BlobContainerClient destContainerClient \= destBlobServiceClient.GetBlobContainerClient(destContainerName);

// Get a BlobClient for the destination blob (same name as the source blob)
BlobClient destBlobClient \= destContainerClient.GetBlobClient(sourceBlobName);

// Copy the blob
await destBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);

// Check if the blob copy has been completed or not
while (destBlobClient.GetProperties().Value.CopyStatus == Azure.Storage.Blobs.Models.CopyStatus.Pending)
{
// If the copy hasn't been completed, wait a bit then check again
await Task.Delay(1000);
}

// After the blob has been copied to the destination, delete it from the source
await sourceBlobClient.DeleteIfExistsAsync();