CloudBlob.DownloadToStream returns null

2019-04-19 14:36发布

问题:

I'm trying to download a file from cloudBlob via stream. I refer to this article CloudBlob

Here is the code to download the 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;
}  

And the code to convert it into byte array

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

But I always get null value. Below is the content of the streamed file.

What is wrong with this? Please help.

EDIT

Setting the Position to 0 inside ReadFully method is not allowed, so I put it inside DownloadBlobAsStream

This should work now:

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

回答1:

Your problem is that your input stream pointer is set to end of the steam (See the screen shot, Length and Position both shows same value) that's why when you read it you always get null. You would need to set to input stream pointer to 0 using Stream.Position = 0 as below:

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


回答2:

How about using the OpenRead() method on the CloudBlob object?

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


回答3:

I have tried implementing the code above, but to my suprise, the function GetBlockBlobReference was not present in CloudBlobClient but in CloudBlockBlob.

Maybe the DLLs changed through time.

Therefore I present you with my adaptation:

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


}