Using Azure Blob Storage as file storage solution

Introduction

Microsoft Azure Blob Storage is a cloud-based storage solution that allows you to store and retrieve different types of data, including documents, images, and videos. Whether it’s for personal or enterprise use, Azure Blob Storage offers a scalable and cost-effective solution for storing your files with ease.

In this tutorial, we’ll show you how to use Azure Blob Storage as a file storage solution for your web application or cloud-based project. We’ll also cover some of the basic concepts and features of Azure Blob Storage, its pricing, and how to interact with it using the Azure Portal, Azure Storage Explorer, and Azure Blob Storage SDK for .NET.

Prerequisites

Before we start, there are a few prerequisites that you’ll need to get started:

  • An Azure account
  • Access to the Azure Portal
  • A basic understanding of the Azure Storage Service

Concepts of Azure Blob Storage

Azure Blob Storage is a cloud-based storage solution that allows you to store and retrieve different types of data. It is part of the Azure Storage Service, which also includes Azure Files and Azure Disks for file and disk-based storage, respectively.

Blob Containers

Blob Containers are used to group a set of blobs together. They are similar to folders in a file system, but they don’t define a hierarchy. You can have any number of Blob Containers, and each Blob Container can store any number of blobs.

Blobs

Blobs (Binary Large Objects) are the basic units of data that can be stored in Azure Blob Storage. They can contain any kind of data, including text, images, videos, and audio. Blobs can be up to 5 TB in size.

Access Tiers

Azure Blob Storage offers multiple storage tiers for your data, each with its own pricing and performance characteristics. The available access tiers are:

  • Hot access tier: This tier is optimized for frequently accessed data that is required to be instantly available. The pricing for the hot access tier is higher than the others.
  • Cool access tier: This tier is optimized for infrequently accessed data that is required to be available within a few hours. The pricing for the cool access tier is lower than the hot access tier.
  • Archive access tier: This tier is optimized for data that is rarely accessed and can tolerate longer retrieval times. The pricing for the archive access tier is the lowest of the three.

Pricing

Azure Blob Storage pricing is based on three main factors:

  • The amount of data stored
  • The amount of data transferred in and out of Blob Storage
  • The type of access tier chosen for the data

Creating a Blob Container in Azure Portal

Before we can store any data in Azure Blob Storage, we need to create a Blob Container in the Azure Portal.

  1. Log in to the Azure portal with your credentials.
  2. In the left-hand menu, click on “Storage accounts” and select the storage account you want to use. If you haven’t created a storage account yet, follow these instructions to create one.
  3. In the storage account menu, click on “Blob service” and then click on “Containers”.
  4. Click on the “+ Container” button to create a new container.
  5. Provide a name for the container and select the access tier. You can also choose to make the container public or private. Click on “Create” to create the container.

Congratulations! You have created your first Blob Container.

Uploading files to Blob Container

Now that we have a Blob Container, we can start uploading files to it. There are several ways to upload files to a Blob Container, including the Azure Portal, Azure Storage Explorer, and Azure Blob Storage SDK. In this tutorial, we’ll show you how to upload files using the Azure Portal.

  1. In the Azure Portal, navigate to your Blob Container.
  2. Click on the “+ Add” button to upload a file.
  3. In the “Upload blob” menu, click on “Select file”.
  4. Browse your computer for the file you want to upload, and select it.
  5. Add any metadata you want to attach to the file, such as a description or author name.
  6. Click on “Upload” to upload the file to the Blob Container.

Congratulations! You have uploaded your first file to Azure Blob Storage.

Accessing files stored in Blob Container

Now that we have uploaded a file to the Blob Container, we can access it from our web application or cloud-based project. There are several ways to access files stored in Azure Blob Storage, including using SAS (Shared Access Signature) tokens, signed URLs, or Azure Blob Storage SDK. In this tutorial, we’ll show you how to access files using SAS tokens.

Shared Access Signature (SAS) Tokens

A Shared Access Signature (SAS) token is a URI that contains a set of query parameters that define the access rights and expiration time for a resource in Azure Blob Storage. You can use SAS tokens to grant temporary access to your Blob Container or individual blobs.

  1. In the Azure Portal, navigate to your Blob Container.
  2. Click on the “Access policy” menu and select the type of access policy you want to create. You can create an access policy that grants read, write, or delete access, or all of those.
  3. Set the start and expiry time for the access policy. The start time is optional, while the expiry time is mandatory.
  4. Click on “Generate SAS and connection string”.
  5. Click on “Copy” to copy the SAS token URI to your clipboard.

Now you can use the SAS token URI to access your Blob Container or individual blobs from your web application or cloud-based project.

Using Azure Blob Storage SDK for .NET

Azure Blob Storage SDK offers a comprehensive solution for interacting with Blob Storage directly from your .NET code. You can use the SDK to perform various tasks, such as uploading and downloading blobs, listing blobs in a container, and deleting blobs.

Installing Azure Blob Storage SDK

You can install Azure Blob Storage SDK for .NET using the NuGet package manager in Visual Studio.

  1. Open your Visual Studio project.
  2. Right-click on the project name in the Solution Explorer, and select “Manage NuGet packages”.
  3. In the “Browse” tab, search for “Azure.Storage.Blobs” package.
  4. Click on “Install” to install the package.

Uploading a Blob using Azure Blob Storage SDK

Now that we have installed the Azure Blob Storage SDK, let’s see how to upload a blob using .NET code.

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

public class BlobStorageHelper
{
    private string connectionString;
    private string containerName;

    public BlobStorageHelper(string connectionString, string containerName)
    {
        this.connectionString = connectionString;
        this.containerName = containerName;
    }

    public async Task UploadBlobAsync(string blobName, Stream content)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        await blobClient.UploadAsync(content);
    }
}

In the code snippet above, we have defined a class called BlobStorageHelper. The constructor of the class takes two parameters connectionString and containerName, which are used to create a BlobContainerClient instance.

The UploadBlobAsync method takes two parameters: blobName and content. The blobName parameter is the name of the blob, while the content parameter is the content of the blob as a stream.

We create a BlobClient instance from the BlobContainerClient using the GetBlobClient method. We then call the UploadAsync method on the BlobClient instance to upload the blob to the Blob Container.

Conclusion

Azure Blob Storage is a powerful storage solution that offers a scalable and cost-effective way to store and retrieve different types of data. In this tutorial, we have covered the basic concepts and features of Azure Blob Storage, how to create a Blob Container, upload files to it, and access files stored in it. We have also shown you how to interact with Azure Blob Storage using the Azure Portal, Azure Storage Explorer, and Azure Blob Storage SDK for .NET.

Whether it’s for personal or enterprise use, Azure Blob Storage is a great way to store your files with ease. With its secure and reliable storage, you can be sure that your data is safe and accessible whenever you need it.

Related Post