CloudBlob.OpenRead()不读取所有数据使用WindowsAzureStorage客户端库,CloudBlob.OpenRead()方法只读取4MB的数据。如何使用OpenRead方法读取完整流。CloudBlobblob=container.GetBlobReference(文件名);流stream=blob.OpenRead();我必须使用OpenRead方法。不能使用DownloadToFile或DownloadToStream。编辑:我的范围之外的消费者代码调用stream.CopyTo(readIntoStream);CopyTo是一种扩展方法。publicstaticintCopyTo(thisStreamsource,Streamdestination){byte[]buffer=newbyte[BUFFER_SIZE];intbytesRead;intbytesCopied=0;做{bytesRead=source.Read(buffer,0,BUFFER_SIZE);如果(bytesRead>0)destination.Write(buffer,0,bytesRead);bytesCopied+=bytesRead;}while(bytesRead>0);返回字节已复制;编辑2:我观察到当使用blob.UploadText()方法上传文件时,它工作正常但是当使用OpenWrite方法上传blob时,如下面的代码示例所示,OpenRead方法只读取4194304字节(4姆)。using(varinput=File.OpenRead(@"C:testFile.txt"))//5000000字节using(varoutput=blob.OpenWrite()){input.CopyTo(output);编辑3:在我最后产生问题的完整代码。staticvoidMain(string[]args){varblobContainer=GetContainer("tier1");blobContainer.CreateIfNotExist();varcontainerPermissions=newBlobContainerPermissions();containerPermissions.PublicAccess=BlobContainerPublicAccessType.Blob;blobContainer.SetPermissions(containerPermissions);varblob=blobContainer.GetBlobReference("testfileWithOpenWrite1.txt");using(varinput=File.OpenRead(@"C:testFile.txt"))//5000000字节using(varoutput=blob.OpenWrite(newBlobRequestOptions()))input.CopyTo(output);Console.WriteLine("已上传");intbytesDownloaded=0;字节[]缓冲区=新字节[65536];使用(BlobStreambs=blob.OpenRead()){intchunkLength;做{chunkLength=bs.Read(buffer,0,buffer.Length);bytesDownloaded+=块长度;}while(chunkLength>0);}Console.WriteLine(bytesDownloaded);}publicstaticintCopyTo(此流源,流目标){intBUFFER_SIZE=65536;byte[]buffer=newbyte[BUFFER_SIZE];在t字节读取;intbytesCopied=0;做{bytesRead=source.Read(buffer,0,BUFFER_SIZE);如果(bytesRead>0)destination.Write(buffer,0,bytesRead);bytesCopied+=bytesRead;}while(bytesRead>0);返回字节已复制;}编辑4-结论:这可能是SDKv1.2附带的Microsoft.WindowsAzure.StorageClient.dll(程序集版本6.0.6002.17043)中的一个错误它适用于最新的Microsoft.WindowsAzure.StorageClient。dll(程序集版本6.0.6002.18312-SDKv1.4)。谢谢smarx?我无法重现你所看到的。事情似乎按预期工作:staticvoidMain(string[]args){//我还尝试了一个真正的云存储帐户。同样的结果。varcontainer=CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient().GetContainerReference("testcontainer");容器。CreateIfNotExist();varblob=container.GetBlobReference("testblob.txt");blob.UploadText(newString('x',5000000));varsource=blob.OpenRead();intBUFFER_SIZE=4000000;byte[]buffer=newbyte[BUFFER_SIZE];intbytesRead;intbytesCopied=0;做{bytesRead=source.Read(buffer,0,BUFFER_SIZE);bytesCopied+=bytesRead;}while(bytesRead>0);Console.WriteLine(bytesCopied);//打印5000000}编辑:我也(在回答编辑过的问题时)现在尝试使用OpenWrite上传blob,结果相同。(我得到了完整的blob。)我使用这段代码而不是blob.UploadText(...):using(varinput=File.OpenRead(@"testblob.txt"))//5000000bytesusing(varoutput=blob.OpenWrite()){输入.CopyTo(输出);}最后的WriteLine仍然打印5000000。编辑2:这有点烦人。将BUFFER_SIZE更改为65536没有任何改变。结果对我来说似乎仍然正确。下面是我完整的对比应用:以上是C#学习教程:CloudBlob.OpenRead()没有看完所有资料分享的内容,如果对大家有用需要详细了解C#学习教程,希望你多注意点—staticvoidMain(string[]args){//我也尝试了一个真正的云存储帐户。同样的结果。var容器=CloudStorageAccount。开发存储帐户。创建CloudBlobClient()。GetContainerReference("测试容器");blob=container.GetBlobReference("testblob.txt");using(varinput=File.OpenRead(@"testblob.txt"))//5000000字节using(varoutput=blob.OpenWrite()){input.CopyTo(output);}varsource=blob.OpenRead();intBUFFER_SIZE=65536;byte[]buffer=newbyte[BUFFER_SIZE];intbytesRead;intbytesCopied=0;做{bytesRead=source.Read(buffer,0,BUFFER_SIZE);bytesCopied+=bytesRead;}while(bytesRead>0);Console.WriteLine(bytesCopied);//prints5000000}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
