1

总而言之,我正在尝试找到一种从 Windows azure 读取 blob 的方法。现在我遇到了一些问题,读取的块的总大小不等于 blob 的总大小。我的测试 blob 大小为 154805720 字节,每个块读取大小为 10*1024*1024。我发现块读取的最后一个缓冲区不是假定的大小 8005080 字节。它总是 4M。顺便说一句,我已通过 cloudbrerry 存储资源管理器将此 blob 下载到本地。它与我之前上传的原始文件大小相同。所以,我确定 blob 原始大小是可以的,这意味着 154805720 字节。这是我的代码。请帮助查看它。

        private static CloudBlobClient CreateBlobClient(StorageAccount account)
        {
            CloudBlobClient blobClient = null;
            CloudStorageAccount oStorageAccount = CreateStorageAccount(account);
            blobClient = oStorageAccount.CreateCloudBlobClient();
            blobClient.Timeout = new TimeSpan(2, 0, 0);
            blobClient.WriteBlockSizeInBytes = 4 * 1024 * 1024;
            blobClient.RetryPolicy = RetryPolicies.Retry(20, TimeSpan.Zero);

            return blobClient;
        }



        public static byte[] DownloadChunkFromBlob(StorageAccount account, string sContainerName, String sBlobName, int blobOffset, int bufferSize)
        {
            CloudBlobClient blobClient = CreateBlobClient(account);

            CloudBlobContainer container = blobClient.GetContainerReference(sContainerName);
            bool b = container.CreateIfNotExist();
            CloudBlob blob = container.GetBlobReference(sBlobName);

            using (var blobStream = blob.OpenRead())
            {
                var buffer = new byte[bufferSize];
                blobStream.Seek(blobOffset, SeekOrigin.Begin);
                int numBytesRead = blobStream.Read(buffer, 0, bufferSize);

                if (numBytesRead != bufferSize)
                {
                    var trimmedBuffer = new byte[numBytesRead];
                    Array.Copy(buffer, trimmedBuffer, numBytesRead);
                    return trimmedBuffer;
                }
                return buffer;
            }
        }
4

1 回答 1

0

答案是OpenRead()一次最多只能读取 4 MB。当我设置块读取大小为 4MB 。一切安好。

于 2012-11-01T03:56:38.247 回答