Как добавить файл в Азурите [Azure Эмулятор хранения] - PullRequest
0 голосов
/ 04 августа 2020

Мне сложно понять, как добавить файл в этот эмулятор, чтобы локально протестировать azure blob различные операции.

1 Ответ

0 голосов
/ 05 августа 2020

После установки азурита вам необходимо запустить его вручную.

enter image description here

There are two ways to connect to Azurite:

1. enter image description here

2. enter image description here

enter image description here

The next step I think is the same as using azure storage in the cloud, only need to use sdk for blob operation:

var blobHost = Environment.GetEnvironmentVariable("AZURE_STORAGE_BLOB_HOST");  // 126.0.0.1:10000
var account = Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"); // devstoreaccount1
var container = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONTAINER");
var emulator = account == "devstoreaccount1";
var blobBaseUri = $"https://{(emulator ? $"{blobHost}/{account}" : $"{account}.{blobHost}")}/";
var blobContainerUri = $"{blobBaseUri}{container}";

// Generate random string for blob content and file name
var content = Guid.NewGuid().ToString("n").Substring(0, 8);
var file = $"{content}.txt";

// With container uri and DefaultAzureCredential
// Since we are using the Azure Identity preview version, DefaultAzureCredential will use your Azure CLI token.
var client = new BlobContainerClient(new Uri(blobContainerUri), new DefaultAzureCredential());

// Create container
await client.CreateIfNotExistsAsync();

// Get content stream
using var stream = new MemoryStream(Encoding.ASCII.GetBytes(content));

// Upload blob
await client.UploadBlobAsync(file, stream);

You can refer to this official документ , в нем есть более подробное руководство. Или вы можете обратиться к этому блогу .

...