private List<UploadedBlock> UploadBytesParallel(RemainingBytes remainingBytes, string localFilePath, string containerName, string blobName, bool testMode=false, int parallelFactor = 2)
{
var uploadedBlockList = new ConcurrentBag<UploadedBlock>();
try
{
CloudBlockBlob blob = null;
if (!testMode)
{
var client = AzureHelper.GetCloudBlobClient();
var container = client.GetContainerReference(containerName);
container.CreateIfNotExists();
blob = container.GetBlockBlobReference(blobName);
}
var blockCount =
Math.Round((double) (remainingBytes.EndOffset - remainingBytes.BeginOffset + 1)/
(double) ConfigHelper.SignatureSize, MidpointRounding.AwayFromZero);
var taskList = new List<Task>();
long offset = remainingBytes.BeginOffset;
using (var stream = new FileStream(localFilePath, FileMode.Open))
{
while (offset <= remainingBytes.EndOffset)
{
while (offset <= remainingBytes.EndOffset && taskList.Count < parallelFactor)
{
var sizeToRead = offset + ConfigHelper.SignatureSize <= remainingBytes.EndOffset
? ConfigHelper.SignatureSize
: remainingBytes.EndOffset - offset + 1;
if (sizeToRead > 0)
{
// seek to the offset we need. Dont forget remaining bytes may be bigger than the signature size
// we want to deal with.
stream.Seek(offset, SeekOrigin.Begin);
var bytesToRead = new byte[sizeToRead];
var bytesRead = stream.Read(bytesToRead, 0, (int)sizeToRead);
var t = WriteBytes(offset, bytesRead, bytesToRead, blob, uploadedBlockList, testMode);
taskList.Add(t);
offset += sizeToRead;
}
}
// wait until we've all uploaded.
var waitedIndex = Task.WaitAny(taskList.ToArray());
taskList.RemoveAt(waitedIndex);
}
// wait on remaining tasks.
Task.WaitAll(taskList.ToArray());
}
}
catch (ArgumentException ex)
{
// probably bad container.
Console.WriteLine("Argument Exception " + ex.ToString());
}
finally
{
}
return uploadedBlockList.ToList();
//return uploadedBlockList;
}