Azurite setup guide - ita-social-projects/WhatBackend GitHub Wiki

Contents

Objective

Every attachment file is stored in blob storage, which is normally provided by Azure services. Considering the highly probable problem occurrence with Azure account (subscription payment issues, security settings etc.), it is more reasonable to use local blob storage (at least during development stage). Such feature is provided by Azurite emulator.

Required software

  • Node.js 8.0 or later

Setting up Azurite

  1. Create empty folder where all the data will be stored. Do not create this folder in WHAT project directory, it should be located somewhere else.

  2. Open terminal in this folder.

  3. Execute the following command to install Azurite

    npm install -g azurite

  4. Execute the following command to run Azurite

    azurite

If everything is done correctly, you will see the next output:

Editing connection strings

Open each .env file and replace content after ConnectionStrings__AzureBlobsAccessKey= with

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

This will set up all applications in WHAT project to use predefined default credentials in Azurite local storage.

Testing Azurite (optional)

  1. Run the API, then authorize in Swagger and upload several attachments using any suitable method (add avatar, for example). These newly added files will be stored in Azurite directory.
  2. Create an empty .NET console application.
  3. Add Azure.Storage.Blobs package via NuGet Package Manager.
  4. Add the following code:
using System;
using Azure.Storage.Blobs;

namespace AzureBlobDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            var result = blobServiceClient.GetBlobContainers().AsPages(default, 5);
            foreach(var page in result)
            {
                foreach(var item in page.Values)
                {
                    Console.WriteLine($"Container name: {item.Name} Is Deleted: {item.IsDeleted}");
                    BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(item.Name);
                    var blobs = blobContainerClient.GetBlobs();
                    foreach (var blob in blobs)
                    {
                        Console.WriteLine(blob.Name);
                        BlobClient blobClient = new BlobClient(connectionString, item.Name, blob.Name);
                        blobClient.DownloadTo(blob.Name);
                    }
                }
            }
            Console.ReadKey(true);
        }
    }
}

This will list all containers and blobs, which will also be downloaded to bin of this application.

Sources