Mastering Azure Storage with .NET

A Comprehensive Guide

Merwan Chinta
CodeNx
4 min readOct 27, 2023

--

Introduction

Azure Storage is a fundamental service in the Azure cloud ecosystem, providing scalable and highly available storage solutions. In this article, we’ll explore how to work with Azure Storage using .NET, covering essential concepts and code samples for each Azure Storage service, including Blob Storage, Table Storage, and Queue Storage.

Azure storage services provide Blob storage, Table storage, File Storage, Queue storage and Disk storage.

Blob vs Table vs Queue Storage

Azure offers several storage services, each designed for specific use cases. Here’s a comparison of Azure Blob Storage, Azure Table Storage, and Azure Queue Storage, along with guidance on when to choose each one:

Azure Blob Storage

Use Case: Azure Blob Storage is ideal for storing unstructured data, such as images, videos, documents, backups, and other large binary or text data. It is often used for content delivery, media streaming, and data archival.

Key Features

  • Supports binary and text data.
  • Scalable for large and unstructured data.
  • Can be used for static website hosting.
  • Offers lifecycle management for data retention policies.
  • Blob access tiers (hot, cool, archive) for cost optimization.

When to Choose

  • Choose Azure Blob Storage when you need to store, serve, or archive large amounts of unstructured data, like media files, backups, or any binary data.
  • Use it when you require public or private content delivery through a URL or CDN.
  • Opt for Blob Storage when you want to host a static website or support data archival requirements.

Azure Table Storage

Use Case: Azure Table Storage is a NoSQL data store suited for semi-structured data. It is a good choice for applications that need to store and query data using a key or index.

Key Features

  • Supports structured data with a schema-less design.
  • Scalable and cost-effective for storing large volumes of data.
  • Well-suited for storing simple entities with properties.
  • Query capabilities through OData.

When to Choose

  • Select Azure Table Storage when your application requires structured data storage without complex relational requirements.
  • It’s suitable for scenarios like IoT data storage, storing user profiles, and managing semi-structured data.
  • Use it when you need fast read access to a large volume of data.

Azure Queue Storage

Use Case: Azure Queue Storage is a message-based service used for decoupling components of cloud applications and enabling asynchronous communication.

Key Features

  • Supports sending, receiving, and processing messages.
  • Ensures message durability and at-least-once message delivery.
  • Provides message expiration and time-to-live features.
  • Ideal for creating fault-tolerant and scalable cloud applications.

When to Choose

  • Choose Azure Queue Storage when you need asynchronous messaging to decouple components in your application.
  • It’s suitable for building distributed, fault-tolerant systems with reliability and load leveling.
  • Use it when you want to enable background processing, task scheduling, or event-driven workflows.

Selecting the Right Storage Service

  • Consider your application’s data requirements: structured vs. unstructured, large binary data vs. small key-value pairs.
  • Think about the scalability needs of your application. Blob Storage and Table Storage are more suitable for large datasets, while Queue Storage is used for asynchronous communication and scaling.
  • Evaluate cost considerations. Blob Storage has different access tiers for cost optimization, and Table Storage is cost-effective for structured data.
  • Think about data access patterns. If you need query capabilities, Table Storage is suitable, while Queue Storage is for message-based communication.

In many real-world scenarios, applications use a combination of these storage services to meet various data and communication requirements. The choice depends on your specific application’s use case and requirements.

Prerequisites

Let’s dive into code samples:

  • Azure subscription with Azure Storage account created.
  • Visual Studio or any .NET development environment.

Azure Blob Storage Code Sample

Azure Blob Storage is used for storing and managing unstructured data. It’s commonly employed for storing documents, images, videos, and more.

Code Sample: Uploading a Blob to Azure Blob Storage. The code will upload the specified file to your Azure Blob Storage container.

using Azure.Storage.Blobs;
using System;
using System.IO;

class Program
{
static async Task Main(string[] args)
{
string connectionString = "YOUR_CONNECTION_STRING";
string containerName = "mycontainer";
string blobName = "myblob.txt";

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);

using FileStream fs = File.OpenRead("path-to-your-file.txt");
await blobClient.UploadAsync(fs, true);
}
}

Azure Table Storage Code Sample

Azure Table Storage is a NoSQL data store used for semi-structured data. It’s ideal for storing data that can be queried quickly.

Code Sample: Inserting an Entity into Azure Table Storage. The code will insert an entity into your Azure Table Storage

using Azure.Data.Tables;
using System;

class Program
{
static async Task Main(string[] args)
{
string connectionString = "YOUR_CONNECTION_STRING";
string tableName = "mytable";

TableServiceClient serviceClient = new TableServiceClient(connectionString);
TableClient tableClient = serviceClient.GetTableClient(tableName);

MyEntity entity = new MyEntity("PartitionKey", "RowKey")
{
Property1 = "Value1",
Property2 = "Value2"
};

await tableClient.AddEntityAsync(entity);
}
}

public class MyEntity : ITableEntity
{
// Define entity properties here
}

Azure Queue Storage Code Sample

Azure Queue Storage is a message-based service for decoupling and scaling components of a cloud application.

Code Sample: Enqueue and Dequeue Messages in Azure Queue Storage. The code will enqueue a message into your Azure Queue Storage and then dequeue and print the message.

using Azure.Storage.Queues;
using System;

class Program
{
static async Task Main(string[] args)
{
string connectionString = "YOUR_CONNECTION_STRING";
string queueName = "myqueue";

QueueServiceClient serviceClient = new QueueServiceClient(connectionString);
QueueClient queueClient = serviceClient.GetQueueClient(queueName);

await queueClient.SendMessageAsync("Hello, Azure Queue!");

// Dequeue a message
QueueMessage[] messages = await queueClient.ReceiveMessagesAsync(1);
foreach (QueueMessage message in messages)
{
Console.WriteLine(message.MessageText);
await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
}
}
}

Remember to replace “YOUR_CONNECTION_STRING” and other placeholder values in the code samples with your actual Azure Storage connection string and container/table/queue names.

Conclusion

Azure Storage services offer a wide range of capabilities for managing data in the cloud. In this article, we’ve explored Azure Blob Storage for file storage, Azure Table Storage for structured data, and Azure Queue Storage for message-based communication. With these code samples and explanations, you can kick-start your journey into leveraging Azure Storage with .NET for your applications.

--

--

Merwan Chinta
CodeNx

🚧 Roadblock Eliminator & Learning Advocate 🖥️ Software Architect 🚀 Efficiency & Performance Guide 🌐 Cloud Tech Specialist