我正在尝试通过流从 cloudBlob 下载文件。我参考这篇文章CloudBlob
这是下载blob的代码
public Stream DownloadBlobAsStream(CloudStorageAccount account, string blobUri)
{
Stream mem = new MemoryStream();
CloudBlobClient blobclient = account.CreateCloudBlobClient();
CloudBlockBlob blob = blobclient.GetBlockBlobReference(blobUri);
if (blob != null)
blob.DownloadToStream(mem);
return mem;
}
以及将其转换为字节数组的代码
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
但我总是得到空值。以下是流文件的内容。
这有什么问题?请帮忙。
编辑
ReadFully
不允许在方法内部设置Position为0 ,所以我把它放在里面DownloadBlobAsStream
现在应该可以了:
public Stream DownloadBlobAsStream(CloudStorageAccount account, string blobUri)
{
Stream mem = new MemoryStream();
CloudBlobClient blobclient = account.CreateCloudBlobClient();
CloudBlockBlob blob = blobclient.GetBlockBlobReference(blobUri);
if (blob != null)
blob.DownloadToStream(mem);
mem.Position = 0;
return mem;
}