Azurite setup guide - ita-social-projects/WhatBackend GitHub Wiki
Contents
- Objective
- Required software
- Setting up Azurite
- Editing connection strings
- Testing Azurite (optional)
- Sources
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
-
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.
-
Open terminal in this folder.
-
Execute the following command to install Azurite
npm install -g azurite
-
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)
- 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.
- Create an empty .NET console application.
- Add Azure.Storage.Blobs package via NuGet Package Manager.
- 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.