使用 Windows Azure 存储客户端库,CloudBlob.OpenRead() 方法仅读取 4 mb 的数据。如何使用 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;
}
编辑2:
我观察到,当使用 blob.UploadText() 方法上传文件时,它工作正常,但是当使用 OpenWrite 方法上传 blob 时(如以下代码示例所示),OpenRead 方法仅读取 4194304 字节 (4 mb)。
using(var input = File.OpenRead(@"C:\testFile.txt")) //5000000 bytes
using (var output = blob.OpenWrite())
{
input.CopyTo(output);
}
编辑3:
在我这里产生问题的完整代码。
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(程序集版本 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。)我使用此代码代替 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
}
关于c# - CloudBlob.OpenRead() 未读取所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911728/