openread()没有读取所有数据
本文关键字:读取 数据 openread | 更新日期: 2023-09-27 18:04:29
对于windows Azure存储客户端库,CloudBlob.OpenRead()方法仅读取4mb的数据。如何使用OpenRead方法读取完整流。
CloudBlob blob = container.GetBlobReference(filename);
Stream stream = blob.OpenRead();
我必须使用OpenRead方法。不能使用DownloadToFile或DownloadToStream.
编辑:在我的作用域之外的消费者代码调用
stream.CopyTo(readIntoStream);
CopyTo是一个扩展方法。
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;
}
EDIT 2:
我观察到,当文件使用blob. uploadtext()方法上传时,它工作得很好,但是当blob使用OpenWrite方法上传时,如以下代码样例所做,OpenRead方法仅读取4194304字节(4mb)。
using(var input = File.OpenRead(@"C:'testFile.txt")) //5000000 bytes
using (var output = blob.OpenWrite())
{
input.CopyTo(output);
}
EDIT:
在我的结尾产生问题的完整代码。
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;
}
EDIT 4 - Conclusion:
这可能是SDK v1.2附带的Microsoft.WindowsAzure.StorageClient.dll(程序集版本6.0.6002.17043)中的错误。它适用于最新的Microsoft.WindowsAzure.StorageClient.dll(程序集版本6.0.6002.18312 - SDK v1.4)。
谢谢smarx:)
我无法重现你所看到的。事情似乎如预期的那样运行:
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(…):
using (var input = File.OpenRead(@"testblob.txt")) //5000000 bytes
using (var output = blob.OpenWrite())
{
input.CopyTo(output);
}
最后的WriteLine仍然打印5000000。
编辑2:这有点烦了。将BUFFER_SIZE更改为65536不会改变任何东西。结果在我看来仍然是正确的。下面是我的完整的比较应用程序:
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
}