CloudBlob.DownloadToStream返回null

我正在尝试通过流从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; } 

你的问题是你的输入流指针被设置为蒸汽结束(参见屏幕截图,长度和位置都显示相同的值)这就是为什么当你读它时你总是得到null。 您需要使用Stream.Position = 0将输入流指针设置为0 ,如下所示:

 public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[16 * 1024]; input.Position = 0; // Add this line to set the input stream position to 0 using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } 

如何在CloudBlob对象上使用OpenRead()方法?

 public static string ReadFully(string blobUri, string itemUri) { // eg itemUri == "foo.txt" // if there is a folder "bar" with foo.txt, provide instead: "bar/foo.txt" CloudBlobContainer cloudBlobContainer = new CloudBlobContainer(new Uri(blobUri)); CloudBlob blobReference = cloudBlobContainer.GetBlobReference(itemUri); using (var stream = blobReference.OpenRead()) { using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } 

我尝试过实现上面的代码,但令我惊讶的是,函数GetBlockBlobReferenceCloudBlobClient中并不存在于CloudBlobClient中。

也许DLL随着时间的推移而改变。

所以我告诉你我的改编:

 public class BlobStorageHelper { private readonly CloudBlobClient _blobClient; protected readonly CloudStorageAccount StorageAccount; public string _containerName { get; set; } public BlobStorageHelper() { _blobClient = base.StorageAccount.CreateCloudBlobClient(); _containerName = ConfigurationManager.AppSettings["StorageContainerName"]; StorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); } protected Stream DownloadBlobAsStream(string blobUri) { CloudStorageAccount account = this.StorageAccount; CloudBlockBlob blob = GetBlockBlobReference(account, blobUri); Stream mem = new MemoryStream(); if (blob != null) { blob.DownloadToStream(mem); } return mem; } private CloudBlockBlob GetBlockBlobReference(CloudStorageAccount account, string blobUri) { string blobName = blobUri.Substring(blobUri.IndexOf("/" + _containerName + "/")).Replace("/" + _containerName + "/", ""); CloudBlobClient blobclient = account.CreateCloudBlobClient(); CloudBlobContainer container = _blobClient.GetContainerReference(_containerName); container.CreateIfNotExists(); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); return blob; } public byte[] DownloadBlobAsByeArray(string blobUri) { Stream inputStream = DownloadBlobAsStream(blobUri); byte[] buffer = new byte[16 * 1024]; inputStream.Position = 0; // Add this line to set the input stream position to 0 using (MemoryStream ms = new MemoryStream()) { int read; while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } }