Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting image from azure blob storage to Base64?

I'm trying to convert image from Azure blob storage to base64:

private static string FromAzureToBase64(string azureUri)
{
    Uri blobUri = new Uri(azureUri);
    CloudBlockBlob blob = new CloudBlockBlob(blobUri, StorageAccount.Credentials);

    byte[] arr = new byte[blob.Properties.Length];
    blob.DownloadToByteArray(arr, 0);
    var azureBase64 = Convert.ToBase64String(arr);
    return azureBase64;
}

The problem with arr parameter is that I should define its length, but the value of blob.Properties.Length is -1, however the image exists on Azure, but almost all its properties either null or unspecified:

enter image description here

like image 498
mshwf Avatar asked Nov 26 '25 12:11

mshwf


1 Answers

What you could do is fetch the blob's properties and then blob's length property will be populated. So your code would be:

private static string FromAzureToBase64(string azureUri)
{
    Uri blobUri = new Uri(azureUri);
    CloudBlockBlob blob = new CloudBlockBlob(blobUri, StorageAccount.Credentials);
    blob.FetchAttributes();//Fetch blob's properties
    byte[] arr = new byte[blob.Properties.Length];
    blob.DownloadToByteArray(arr, 0);
    var azureBase64 = Convert.ToBase64String(arr);
    return azureBase64;
}
like image 127
Gaurav Mantri Avatar answered Nov 28 '25 04:11

Gaurav Mantri