Chunk Read Issue in Windows Azure Blob
-
13-07-2021 - |
سؤال
All, I am trying to find a way to chunk read blob from windows azure .now I had some problem with it , the total size of chunk read is not equal to the total size of the blob. My test blob size is 154805720 bytes, and every chunk reading size is 10*1024*1024. I found the last buffer of chunk read is not the supposed size 8005080 bytes .It is always 4M. BTW, I have downloaded this blob to local by cloudbrerry storage explorer. it is same size with the original file I uploaded before. so ,I am sure the blob original size is ok ,that means 154805720 bytes. Here is my code .please help review it .
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;
}
}
المحلول
The answer is OpenRead()
can only read up to 4 MB in a single go. When I set the chunk read size is 4MB . Everything is Fine.