15

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

3 回答 3

13

您的问题是您的输入流指针设置为结束(参见屏幕截图,长度和位置都显示相同的值),这就是为什么当您阅读它时,您总是得到空值。您需要使用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();
    }
} 
于 2012-06-05T06:22:55.797 回答
10

在 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();
        }
    }
}
于 2016-08-09T22:49:32.390 回答
0

我已经尝试实现上面的代码,但令我惊讶的是,该函数GetBlockBlobReference不存在CloudBlobClientCloudBlockBlob.

也许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();
        }
    }


}
于 2017-09-08T15:02:31.013 回答