Windows Azure Blob中的块读取问题

时间:2012-10-20 08:48:37

标签: azure azure-storage azure-storage-blobs

所有,我试图找到一种从windows azure中读取块的方法。现在我遇到了一些问题,块读取的总大小不等于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;
            }
        }

1 个答案:

答案 0 :(得分:0)

答案是OpenRead()一次只能读取4 MB。当我设置块的读取大小是4MB。一切都很好。