Azure

Microsoft Azure Storage Blob Files Upload, Download And Delete In Web API

Introduction

In this article, I have explained Azure Blob storage, and it will help you to understand how to store, download, and delete files from Azure storage.

Basically, it’s a Microsoft object storage solution for the cloud and helps you to store huge amounts of unstructured data. Unstructured data are noting but, it is text and binary data.

Azure container name must be lowercase.

Microsoft Azure Storage supports three types of blobs,

  1. Block blobs
    It will store the text and binary data’s and individually managed the blocks of data with blobs. Normally block blobs store up to 190.7 TiB.
     
  2. Append blobs
    It is like block blobs. But, optimized for the append operations and append blobs ideal scenarios in logging data from virtual machines.
     
  3. Page blobs
    Page blobs store virtual hard drive files and serve as disks for Azure virtual machines, and they will store the random-access files up to 8 TiB

Prerequisites

  • Install-Package Microsoft.Azure.Storage.Blob -Version 11.2.3

Upload the files in Microsoft Azure storage blob

Normally you can upload the files with help of the UploadFromStream() method in the blobs storage, below code will help you to upload the files in the Azure storage blob with c# in the web API project. You should know the Container Name, Archive Storage Account, Storage Key to upload the files in Azure Storage Blob.

byte[] imageBytes = Convert.FromBase64String("Your Server Image");
string BlobContainerName = "Your Blob BlobContainerName";
string AzureArchiveStorageAccount = "Your Blob AzureArchiveStorageAccount";
string AzureArchiveStorageKey = "Your Blob AzureArchiveStorageKey";
using(var stream = new MemoryStream(imageBytes)) {
    StorageCredentials credentials = new StorageCredentials(AzureArchiveStorageAccount, AzureArchiveStorageKey);
    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(BlobContainerName);
    CloudBlockBlob cblob = cloudBlobContainer.GetBlockBlobReference("ImageName.png");
    cblob.UploadFromStream(stream);
}

Download the files in Microsoft Azure storage blob

You can download the files with the help of DownloadToStream() method in the blobs. The below code will help you to download the files in the Azure storage blob with c# in the web API project. You should know the Container Name, Archive Storage Account, Storage Key to upload the files in Azure Storage Blob.

string BlobContainerName = "Your Blob BlobContainerName";
string AzureArchiveStorageAccount = "Your Blob AzureArchiveStorageAccount";
string AzureArchiveStorageKey = "Your Blob AzureArchiveStorageKey";
using(var stream = new MemoryStream(imageBytes)) {
    StorageCredentials credentials = new StorageCredentials(AzureArchiveStorageAccount, AzureArchiveStorageKey);
    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(BlobContainerName);
    CloudBlockBlob cblob = cloudBlobContainer.GetBlockBlobReference("ImageName.png");
    MemoryStream memStream = new MemoryStream();
    cblob.DownloadToStream(memStream);
}

Delete the files in the Microsoft Azure storage blob

You can Delete the files with help of the DeleteIfExists() method in the blobs. The below coding will help you to delete the files in the Azure storage blob with c# language in the web API project. You should know the Container Name, Archive Storage Account, Storage Key to upload the files in Azure Storage Blob.

string BlobContainerName = "Your Blob BlobContainerName";
string AzureArchiveStorageAccount = "Your Blob AzureArchiveStorageAccount";
string AzureArchiveStorageKey = "Your Blob AzureArchiveStorageKey";
using(var stream = new MemoryStream(imageBytes)) {
    StorageCredentials credentials = new StorageCredentials(AzureArchiveStorageAccount, AzureArchiveStorageKey);
    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(BlobContainerName);
    CloudBlockBlob cblob = cloudBlobContainer.GetBlockBlobReference("ImageName.png");
    bool blobExists = cblob.DeleteIfExists();
}

Hopefully, this article has given you sufficient information to start Microsoft Azure storage blob files upload, download, and delete in web API. Feel free to leave a comment if you would like me to further elaborate on anything within this article.

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close