Cloud storage
Last modified on Tue 04 Oct 2022

Cloud services are providing new options when choosing storage for your applications. While there are several provider options available, we will mainly focus on Azure Storage in this handbook.

Azure Storage

Azure Storage is a Microsoft's solution for cloud storages. There are a couple of storage options within the the Azure ecosystem, which will be discussed in this section Official documentation

For the local development, Microsoft has provided Azurite (previously called Azure Storage Emulator). Azurite is an emulator that provides all the features available on Azure Storage: Blob Storage, Queues and Table storages. The fastest way to connect to your local Azure storage emulator is by setting the connection string to UseDevelopmentStorage=true, which is equivalent to:

DefaultEndpointsProtocol=http;
AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;
QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;
TableEndpoint=http://127.0.0.1:10001/devstoreaccount1;

This connection string can be used to connect to Blob, Queue and Table storages. If you want to see what data you added to your local storage, we recommend using Azure Storage Explorer. This tool can also be used to connect to other storage accounts.

Azure Blob Storage

Azure Blob storage is optimized for storing massive amounts of unstructured data like images, documents and other files. You can read more about it here.

Blob storage offers three types of resources:

Access control

Since we use Azure Blob to store images and documents, it is important to manage blobs and containers in a way that allows only authorized users and services to access those files. We can do that in a couple of ways:

Usage example

The main entry point for communication with Azure Blob Storage is BlobServiceClient. There are a couple of ways to create the client, we can use the extension method for IServiceCollection that registers the client. The extension method will register the client in the DI service:

// in Program.cs:
builder.Services.AddAzureClients(
  builder => builder.AddBlobServiceClient(builder.Configuration["BlobConfiguration:ConnectionString"]));

...

public class BlobService
{
  private readonly BlobServiceClient _blobServiceClient;

  public BlobService(BlobServiceClient blobServiceClient)
  {
    _blobServiceClient = blobServiceClient;
  }

  ...
}

Alternatively we could build our own factory that creates the client:

public interface IAzureBlobServiceClientFactory
{
    BlobServiceClient CreateBlobServiceClient();
}

public class AzureBlobOptions
{
    public string ConnectionString { get; set; }
}

public class AzureBlobServiceClientFactory : IAzureBlobServiceClientFactory
{
    private readonly IOptions<AzureBlobOptions> _options;

    public AzureBlobServiceClientFactory(IOptions<AzureBlobOptions> options)
    {
        _options = options;
    }

    public BlobServiceClient CreateBlobServiceClient()
    {
        return new BlobServiceClient(_options.Value.ConnectionString);
    }
}

public class TestService
{
    private readonly BlobServiceClient _client;

    public TestService(IAzureBlobServiceClientFactory azureBlobServiceClientFactory)
    {
        _client = azureBlobServiceClientFactory.CreateBlobServiceClient();
    }

    public async Task CreateContainerAsync(string containerName)
    {
        await _client.CreateContainerAsync(containerName);
    }
}

Azure Table Storage

Azure Table Storage provides a way to store large amounts of structured data. This service is a NoSQL database. We must note that this is not a replacement for SQL database. For more information, please see Understanding the differences between NoSQL and Relational Databases.

Use it when you want to: