blog
Ottorino Bruni  

Getting Started with Azure Blob Storage: A C# Project Using VSCode

Introduction

In my recent articles, I’ve talked extensively about Azure Functions. If you haven’t checked them out yet, I recommend starting withGetting Started with Azure Functions: C# and Visual Studio Code Tutorial.

Today, however, I want to talk to you about another very important and powerful Azure service: Azure Blob Storage.

In this article, I won’t delve into architecture or the Microsoft Azure Well-Architected Framework pillars, but I encourage you to explore them if you aim to build efficient, scalable applications. Instead, this tutorial focuses on demonstrating how straightforward it is to leverage Azure Blob Storage with a library.

What is Azure Blob Storage?

Azure Blob Storage is a specialized service within the broader Azure Storage offering from Microsoft. To fully appreciate what Azure Blob Storage is, it’s essential to understand its place within the Azure Storage ecosystem.

Azure Storage

Azure Storage is a cloud storage solution provided by Microsoft Azure that includes various services designed to meet different storage needs. The key components of Azure Storage are:

  1. Blob Storage: For storing unstructured data like text or binary data.
  2. Table Storage: For storing structured NoSQL data.
  3. Queue Storage: For storing and managing message queues.
  4. File Storage: For shared file storage accessible via the SMB protocol.
Azure Storage Services

Explore Azure Blob storage

Azure Blob Storage is a specific service within Azure Storage designed to store large amounts of unstructured data. This can include text files, images, videos, backups, and logs, making it highly versatile for various use cases. Here are some of the key features and use cases of Azure Blob Storage:

  • Large Data Storage: Ideal for storing large files such as multimedia files, application logs, and backup data.
  • Big Data Analytics: Stores data that can be analyzed using on-premises or Azure-hosted services.
  • Content Delivery: Serves content such as images, videos, and documents directly to users via web applications.
  • Data Backup and Restore: Provides a reliable solution for backing up and restoring data, ensuring high availability and durability.

Azure Storage provides two performance tiers of storage accounts: standard and premium. Each tier offers distinct features and operates on its pricing model.

  • Standard: This tier includes the standard general-purpose v2 account, suitable for most Azure Storage use cases.
  • Premium: Premium accounts deliver enhanced performance through the utilization of solid-state drives (SSDs). Premium accounts offer flexibility by allowing you to choose between three account types: block blobs, page blobs, or file shares.
Azure Storage offers various access tiers for block blob data, each optimized for specific usage patterns to ensure cost-effectiveness:
  • Hot Access Tier: Designed for frequent access, with higher storage costs but lower access costs. New storage accounts default to this tier.
  • Cool Access Tier: Ideal for infrequently accessed data stored for at least 30 days, offering lower storage costs and higher access costs compared to the Hot tier.
  • Cold Access Tier: Optimized for data rarely accessed and stored for a minimum of 90 days, featuring lower storage costs and higher access costs than the Cool tier.
  • Archive Tier: Exclusive to individual block blobs, suitable for data with extended retrieval latency tolerance, remaining in the Archive tier for at least 180 days.

Why Use Azure Blob Storage?

  • Scalability: Easily scales to meet the needs of applications, whether you’re dealing with a few gigabytes or petabytes of data.
  • Accessibility: Accessible from anywhere via HTTP or HTTPS, making it easy to integrate with various applications and frameworks.
  • Security: Offers robust security features, including encryption and fine-grained access controls.

Blob Storage Resources

Azure Blob Storage consists of three types of resources:

  1. Storage Account: This provides a unique namespace in Azure for your data. Every object stored in Azure Storage has an address that includes your unique account name. The account name combined with the Blob Storage endpoint forms the base address for the objects in your storage account.
  2. Containers: Containers are used to organize sets of blobs, functioning similarly to directories in a file system. A storage account can contain an unlimited number of containers.
  3. Blobs: Blobs are the actual data objects stored within containers. A container can store an unlimited number of blobs.

The following diagram illustrates the relationship between a storage account, containers, and blobs:

Blob Storage resources

Let’s Create Our Console App in C# and VSCode

It’s time to create our console app in C# using VSCode to demonstrate how easy it is to use Azure Blob Storage.

Prerequisites:

Let’s walk through creating a console app in C# using VSCode to demonstrate how simple it is to work with Azure Blob Storage. We’ll also add a Microsoft library that will make our job easier.

Step 1: Set Up Your Project

  1. Open VSCode and open a new terminal.
  2. Create a new project by running the following command:
    dotnet new console -o AzureBlobStorageDemo
  3. Navigate to the project directory:
    cd AzureBlobStorageDemo

Step 2: Add the Azure Storage Blobs Client Library

To simplify our interactions with Azure Blob Storage, we’ll use the Azure Storage Blobs client library for .NET.

  1. Install the package using NuGet by running the following command:
    dotnet add package Azure.Storage.Blobs
    

This library supports multiple server versions and is optimized for storing massive amounts of unstructured data, such as text or binary data.

Azure.Storage.Blobs

The following packages contain the classes used to work with Blob Storage data resources:

Azure Blob Storage Demo Code

This C# console app, using the Azure.Storage.Blobs library, demonstrates how to interact with Azure Blob Storage. Here’s a brief description of its functionality:

  1. Set Up Connection: The app initializes a connection to Azure Blob Storage using a provided connection string.
    string connectionString = "YOUR_CONNECTION_STRING";
    var blobServiceClient = new BlobServiceClient(connectionString);
    
  2. Create a Container: A new blob container is created with a unique name.
    string containerName = "otto-storage-account" + Guid.NewGuid().ToString();
    BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);
    Console.WriteLine($"Container {containerName} created!");
    
  3. Create a Temporary File: The app generates a temporary text file with a unique name and writes “Hello Otto” to it.
    string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".txt";
    await File.WriteAllTextAsync(fileName, "Hello Otto");
    
  4. Upload the File: The temporary file is uploaded to the newly created blob container.
    var blobClient = containerClient.GetBlobClient(fileName);
    Console.WriteLine("Uploading file: ", blobClient.Uri);
    await blobClient.UploadAsync(fileName, true);
    
  5. List Blobs in Container: The app lists all blobs in the container, displaying their names.
    System.Console.WriteLine("Listing blobs..."); await foreach (var blobItem in containerClient.GetBlobsAsync())
    {
     System.Console.WriteLine(blobItem.Name);
    }

Finally, you can go to the Azure Portal, navigate to your storage account, and view the newly created container. Inside the container, you will find the text file “Hello Otto” that was uploaded by the app.

Azure Blob Storage Demo Result

Handling Connection Strings

For this demo, the connection string is included in the code for simplicity. However, for real applications, it’s highly recommended to use environment variables, secrets, or Azure Key Vault to manage the security of your application. You can find the connection string in the Azure portal by navigating to your storage account, then selecting Access keys.

Conclusion

Managing and working with files using Azure Blob Storage and the Azure Storage Blobs client library for .NET is easy and efficient. Azure Blob Storage is powerful and versatile, suitable for many different uses.

Benefits of Using Azure Blob Storage

  • Serving Content: Blob Storage is great for showing images or documents directly in a browser.
  • Distributed Access: It allows you to store files for access by many users in different places.
  • Streaming: You can stream video and audio content easily from Blob Storage.
  • Logging: Writing to log files is simple and effective with Blob Storage.
  • Backup and Recovery: Blob Storage is excellent for storing data for backup, restore, disaster recovery, and archiving. This keeps your data safe and easy to recover.
  • Data Analysis: It supports storing data for analysis by services either on-premises or hosted on Azure.

Using Azure Blob Storage for these tasks shows how flexible and easy it is to use. Whether you need to show content to users, share files, or keep backups, Azure Blob Storage is a reliable and scalable solution.

By using the Azure Storage Blobs client library in your projects, you can manage your data easily, taking full advantage of what Azure Blob Storage offers. This makes it a great choice for many applications, showing its strength and flexibility in handling different types of data in the cloud.

If you think your friends or network would find this article useful, please consider sharing it with them. Your support is greatly appreciated.

Thanks for reading! ????

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.