CloudBlob.OpenRead()不读取所有数据 [英] CloudBlob.OpenRead() is not reading all data

查看:133
本文介绍了CloudBlob.OpenRead()不读取所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Windows Azure存储客户端库,CloudBlob.OpenRead()方法读取只有4 MB的数据。我怎样才能读取使用打开读取方法,全码流。

With windows Azure Storage Client Library, CloudBlob.OpenRead() method reads only 4 mb of data. How can I read full stream using OpenRead method.

CloudBlob blob = container.GetBlobReference(filename);
Stream stream = blob.OpenRead();

我必须使用打开读取方法。不能使用DownloadToFile或DownloadToStream。

I must use OpenRead method. Can't use DownloadToFile or DownloadToStream.

编辑: 消费者code这超出我的范围,要求

EDIT : The consumer code which is outside my scope calls

stream.CopyTo(readIntoStream);

CopyTo从是一个扩展方法。

CopyTo is an extension method.

public static int CopyTo(this Stream source, Stream destination)
        {
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            int bytesCopied = 0;

            do
            {
                bytesRead = source.Read(buffer, 0, BUFFER_SIZE);
                if (bytesRead > 0)
                    destination.Write(buffer, 0, bytesRead);
                bytesCopied += bytesRead;
            }
            while (bytesRead > 0);

            return bytesCopied;
        }

编辑2:

我已经观察到,当文件被使用blob.UploadText()方法上载,它工作正常,但是当BLOB使用OpenWrite方法上传为已完成在以下code样品,打开读取方法读取只有4194304字节(4 MB )。

I have observed that when file is uploaded using blob.UploadText() method, it works fine but when blob is uploaded using OpenWrite method as done in following code sample, the OpenRead Method reads only 4194304 bytes (4 mb).

using(var input = File.OpenRead(@"C:\testFile.txt")) //5000000 bytes
using (var output = blob.OpenWrite())
{
       input.CopyTo(output);
}

修改3:

这在我结束产生的问题完全code。

Complete code that produces the issue at my end.

 static void Main(string[] args)
    {
        var blobContainer = GetContainer("tier1");
        blobContainer.CreateIfNotExist();

        var containerPermissions = new BlobContainerPermissions();
        containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
        blobContainer.SetPermissions(containerPermissions);
        var blob = blobContainer.GetBlobReference("testfileWithOpenWrite1.txt");

        using (var input = File.OpenRead(@"C:\testFile.txt")) //5000000 bytes
        using (var output = blob.OpenWrite(new BlobRequestOptions()))
            input.CopyTo(output);

        Console.WriteLine("uploaded");

        int bytesDownloaded = 0;
        byte[] buffer = new byte[65536];

        using (BlobStream bs = blob.OpenRead())
        {
            int chunkLength;
            do
            {
                chunkLength = bs.Read(buffer, 0, buffer.Length);
                bytesDownloaded += chunkLength;
            } while (chunkLength > 0);
        }

        Console.WriteLine(bytesDownloaded);
    }

    public static int CopyTo(this Stream source, Stream destination)
    {
        int BUFFER_SIZE = 65536;
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead;
        int bytesCopied = 0;

        do
        {
            bytesRead = source.Read(buffer, 0, BUFFER_SIZE);
            if (bytesRead > 0)
                destination.Write(buffer, 0, bytesRead);
            bytesCopied += bytesRead;
        }
        while (bytesRead > 0);

        return bytesCopied;
    }

修改4 - 结论:

这可能是随SDK V1.2在Microsoft.WindowsAzure.StorageClient.dll一个bug(程序集版本6.0.6002.17043)。它与最新Microsoft.WindowsAzure.StorageClient.dll(集版本6.0.6002.18312 - SDK 1.4版)。

This was probably a bug in Microsoft.WindowsAzure.StorageClient.dll (assembly version 6.0.6002.17043) that comes with SDK v1.2. It works with latest Microsoft.WindowsAzure.StorageClient.dll (assembly version 6.0.6002.18312 - SDK v1.4).

感谢smarx:)

推荐答案

我无法重现你看到的。事情似乎按预期:

I can't reproduce what you're seeing. Things seem to work as expected:

static void Main(string[] args)
{
    // I also tried a real cloud storage account. Same result.
    var container = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient().GetContainerReference("testcontainer");
    container.CreateIfNotExist();

    var blob = container.GetBlobReference("testblob.txt");

    blob.UploadText(new String('x', 5000000));

    var source = blob.OpenRead();

    int BUFFER_SIZE = 4000000;
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead;
    int bytesCopied = 0;
    do
    {
        bytesRead = source.Read(buffer, 0, BUFFER_SIZE);
        bytesCopied += bytesRead;
    } while (bytesRead > 0);

    Console.WriteLine(bytesCopied); // prints 5000000
}

编辑:

我也(响应编辑的问题),现在上传尝试使用OpenWrite的斑点,结果是一样的。 (我得到了充分的blob回来了。)我的地方blob.UploadText(...)使用这个code:

I've also (in response to the edited question) now tried uploading the blob using OpenWrite, and the result is the same. (I get the full blob back.) I used this code in place of blob.UploadText(...):

using (var input = File.OpenRead(@"testblob.txt")) //5000000 bytes
using (var output = blob.OpenWrite())
{
    input.CopyTo(output);
}

最后的WriteLine仍然打印500万。

The final WriteLine still prints 5000000.

编辑2:

这是有点讨厌。更改BUFFER_SIZE 65536没有改变任何东西。结果似乎仍然正确的给我。下面是我的比较完整的应用程序:

This is getting a bit tiresome. Changing the BUFFER_SIZE to 65536 didn't change anything. The results still seem correct to me. Here's my full application for comparison:

static void Main(string[] args)
{
    // I also tried a real cloud storage account. Same result.
    var container = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient().GetContainerReference("testcontainer");
    container.CreateIfNotExist();

    var blob = container.GetBlobReference("testblob.txt");

    using (var input = File.OpenRead(@"testblob.txt")) //5000000 bytes
    using (var output = blob.OpenWrite())
    {
        input.CopyTo(output);
    }

    var source = blob.OpenRead();

    int BUFFER_SIZE = 65536;
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead;
    int bytesCopied = 0;
    do
    {
        bytesRead = source.Read(buffer, 0, BUFFER_SIZE);
        bytesCopied += bytesRead;
    } while (bytesRead > 0);

    Console.WriteLine(bytesCopied); // prints 5000000
}

这篇关于CloudBlob.OpenRead()不读取所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆