Join Class

How to download and delete image from azure blob

In earlier post i shared how to upload images to azure cloud blob storage, now we see how to download cloud blob images and delete from azure storage.

To get the reference of any particular blob from azure storage, we have to access CloudStorageAccount => CloudBlobClient => CloudBlobContainer => CloudBlob object instance. Learn more about BlobClient Class definition.

download blob image from azure storage

We can find any blob by its file name.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

async Task<IActionResult> DeleteBlobFromCloud(string blobname)

string blobstorageconnection = _configuration.GetSection("AzureStorage")["ConnectionString"];

		CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);

		CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

		string strContainerName = _configuration.GetSection("AzureStorage")["ContainerName"];

		CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);

		var blob = cloudBlobContainer.GetBlobReference(blobname);

		await blob.DeleteIfExistsAsync();
	catch (RequestFailedException ex)
        // write error handling 
		// ex.ToString();
 return Redirect("blobmgr1");

Now call the above delete method from anywhere after checking security, for example, you can use OnGet method to call the delete function like below.

public async void OnGet()

    var _name = Request.Query["name"];          

    if (!string.IsNullOrEmpty(_name))
        CurrentBlobName = _name;
        await DeleteBlobFromCloud(_name);

Download image from azure blob storage

As we already know that all images are stored in azure blob storage in binary format, so we have to get the blob and then convert it to a human readable file format, so in download function we return a FileResult as output.

To convert binary object to blobstream object, we use blobStream = _blockBlob.OpenReadAsync().Result;, blockblob has a built-in method.

Download the blob from cloud storage

public FileResult OnGetDownloadBlobFromCloud(string blobname)

    CloudBlockBlob _blockBlob = null;

    Stream blobStream = null;
    string _containerName = _configuration.GetSection("AzureStorage")["ContainerName"];

    string _blobConnection = _configuration.GetSection("AzureStorage")["ConnectionString"];

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(_blobConnection);

        CloudBlobClient _blobClient = cloudStorageAccount.CreateCloudBlobClient();

        CloudBlobContainer _blobContainer = _blobClient.GetContainerReference(_containerName);
        _blockBlob = _blobContainer.GetBlockBlobReference(blobname);

        blobStream =  _blockBlob.OpenReadAsync().Result;                               
    catch (RequestFailedException ex)
        string _error = ex.ToString();
    return File(blobStream, _blockBlob.Properties.ContentType, _blockBlob.Name);

Above method will return a downloadable file, we just need to call the method from our razor page with correct parameter value.

<a download href="@Url.Page("downloadblob1", "DownloadBlobFromCloud", new { blobname = @Model.CurrentBlobName})">Download Blob</a>

Notice, the method name is "OnGetDownloadBlobFromCloud", but while calling, you have to just call "DownloadBlobFromCloud", not OnGet! on click of the above link if session is valid, the file will be downloaded in local machine.

Azure Cloud Tutorial
learn azure cloud development

Let's learn how to use Azure Cloud Storage system from core c# application.

Data Analysis
learn Artificial Intelligence
Download image from cloud Blob storage
Learn Azure Cloud Development