我正在尝试通过流从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
方法中将位置设置为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;
}
答案 0 :(得分:13)
你的问题是你的输入流指针设置为蒸汽结束(参见屏幕截图,长度和位置都显示相同的值)这就是为什么当你读它时你总是得到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();
}
}
答案 1 :(得分:8)
如何在CloudBlob对象上使用OpenRead()方法?
public static string ReadFully(string blobUri, string itemUri)
{
// e.g. 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();
}
}
}
答案 2 :(得分:0)
我尝试过实现上面的代码,但令我惊讶的是,GetBlockBlobReference
中的函数CloudBlobClient
并不存在于CloudBlockBlob
中。
可能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();
}
}
}