using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.WindowsAzure.StorageClient; namespace HeuristicLab.Clients.Hive.CloudManager.Azure { public static class CloudBlobExtension { private class BlockTransferDetail { public int StartPosition { get; set; } public int BytesToRead { get; set; } public string BlockId { get; set; } } //check if a blob exists // see: http://stackoverflow.com/questions/2642919/checking-if-a-blob-exists-in-azure-storage public static bool Exists(this CloudBlob blob) { try { blob.FetchAttributes(); return true; } catch (StorageClientException ex) { if (ex.ErrorCode == StorageErrorCode.ResourceNotFound) { return false; } else { throw; } } } public static void UploadParallel(this CloudBlockBlob blob, string filePath) { FileInfo file = new FileInfo(filePath); //CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.Name); blob.Properties.ContentType = "application/octet-stream"; // init int blockSize = 262144; long fileSize = file.Length; long leftToRead = fileSize; int startPosition = 0; int blockCount = ((int)Math.Floor((double)(fileSize / blockSize))) + 1; BlockTransferDetail[] transferDetails = new BlockTransferDetail[blockCount]; List blockIdList = new List(); // setup control array for (int j = 0; j < transferDetails.Length; j++) { int toRead = (int)(leftToRead > blockSize ? blockSize : leftToRead); string blockId = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}", j.ToString("0000000")))); transferDetails[j] = new BlockTransferDetail() { StartPosition = startPosition, BytesToRead = toRead, BlockId = blockId }; if (toRead > 0) { blockIdList.Add(blockId); } startPosition += toRead; leftToRead -= toRead; } // perform parallel upload var result = Parallel.For(0, transferDetails.Length, j => { using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[transferDetails[j].BytesToRead]; BinaryReader br = new BinaryReader(fs); fs.Seek(transferDetails[j].StartPosition, SeekOrigin.Begin); br.Read(buffer, 0, transferDetails[j].BytesToRead); if (buffer.Length > 0) { using (MemoryStream ms = new MemoryStream(buffer)) { blob.PutBlock(transferDetails[j].BlockId, ms, null); } } } }); blob.PutBlockList(blockIdList); } public static bool Exists(this CloudBlobContainer blob) { try { blob.FetchAttributes(); return true; } catch (StorageClientException ex) { if (ex.ErrorCode == StorageErrorCode.ResourceNotFound) { return false; } else { throw; } } } } }