CloudBlob.DownloadToStream返回null(CloudBlob.Downloa

2019-07-29 17:44发布

我试图通过流从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();
        }
    }  

但是,我总是得到空值。 下面是流文件的内容。

有什么不对呢? 请帮忙。

编辑

设置里面的位置为0 ReadFully方法是不允许的,所以我把它里面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;
} 

Answer 1:

你的问题是,你的输入流指针设置结束蒸汽(见截屏,长度和位置都显示相同的值),这就是为什么当你读它,你总是空。 则需要使用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();
    }
} 


Answer 2:

如何使用打开读取()方法CloudBlob对象?

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


Answer 3:

我试图实现上面的代码,但我的惊喜,功能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();
        }
    }


}


文章来源: CloudBlob.DownloadToStream returns null