将图像从azure blob存储转换为Base64?

时间:2017-02-21 14:23:32

标签: azure azure-storage

我尝试将图像从Azure blob存储转换为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;
}

arr参数的问题是我应该定义它的长度,但blob.Properties.Length的值是-1,但是图像存在于Azure上,但几乎所有属性都为null或未指定:

enter image description here

1 个答案:

答案 0 :(得分:9)

你可以做的是获取blob的属性,然后填充blob的长度属性。所以你的代码是:

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;
}