...

/

Updating the Code to Add Images to Storage Account

Updating the Code to Add Images to Storage Account

Update the API code to support uploading images to the Storage Account.

We'll cover the following...

Preparing the code

Now that we have set up the storage account, our next step is to update our existing code so that the API can upload files to this storage account. Here’s how:

  • In the csproj file, we will add the following NuGet package.
Press + to interact
<PackageReference Include="Azure.Storage.Blobs" Version="12.15.0" />

This will allow us to use the Azure storage account SDK.

  • Within the ImageDescriberApi.cs file, we will insert the function given below:
Press + to interact
private static async Task UploadFileStreamToStorageAccount(Stream stream, string fileName)
{
// Connection string for the Azure storage account
string connectionString = "{{storageAccountConnectionString}}";
// Name of the blob container where the file will be uploaded
string containerName = "images-backup";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync();
BlobClient blobClient = containerClient.GetBlobClient(fileName);
await blobClient.UploadAsync(stream, true);
}
  • The function, named UploadFileStreamToStorageAccount, performs the following operations:

    • ...