我尝试将图像从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或未指定:
答案 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;
}