Glory Raj
Glory Raj

Reputation: 17701

getting an error while uploading word document to azure storage

I am trying to upload the word document into the azure cloud storage with the blob using the below code

 var fileName = $"{designInput.ProjectNumber}-{projectPhaseData.Name}-BOD.docx";
 var builder = new BasisOfDesignBuilder(UpdatedDesignProject);
 var director = new BasisOfDesignDirector(builder);
 director.BuildBasisOfDesign();
 var document = builder.GetWordDocumentBytes(); // this will give byte array 

 var azureStorageAccountName = Startup.Configuration["AzureStorage:AccountName"]; // 'accountname like storageprod01'
 var azureStorageConnectionString = Startup.Configuration["AzureStorage:ConnectionString"]; // url like DefaultEndpointsProtocol=https;AccountName=storageprod01;AccountKey=abcbbc;EndpointSuffix=core.windows.net

 BlobContainerClient azureStorageContainer = new BlobContainerClient(azureStorageConnectionString, azureStorageAccountName);
 BlobClient blob = azureStorageContainer.GetBlobClient(fileName);
 using var memoryStream = new MemoryStream(document, false);
 blob.Upload(memoryStream);

Getting an error at this line blob.Upload(memoryStream);

Error is

The specified container does not exist

Could any one know why i am getting this error or am i missing something related to azure storage account?

thanks in advance

Upvotes: 0

Views: 214

Answers (1)

Firts you are passing the name of the accountName to the constructor as the name of the Container.(https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobcontainerclient.-ctor?view=azure-dotnet#Azure_Storage_Blobs_BlobContainerClient__ctor_System_Uri_Azure_Storage_StorageSharedKeyCredential_Azure_Storage_Blobs_BlobClientOptions_) enter image description here

And second you have to create the Container before you create the blob.

 BlobContainerClient azureStorageContainer = new BlobContainerClient(azureStorageConnectionString, containerName);
 azureStorageContainer.CreateIfNotExists();
 BlobClient blob = azureStorageContainer.GetBlobClient(fileName);
 using var memoryStream = new MemoryStream(document, false);
 blob.Upload(memoryStream);

Upvotes: 1

Related Questions