Changeset 7563 for branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Azure/CloudBlobExtension.cs
- Timestamp:
- 03/06/12 15:50:21 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Azure/CloudBlobExtension.cs
r7550 r7563 1 1 2 using System; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Text; 6 using System.Threading.Tasks; 2 7 using Microsoft.WindowsAzure.StorageClient; 3 8 namespace HeuristicLab.Clients.Hive.CloudManager.Azure { 4 9 public static class CloudBlobExtension { 10 private class BlockTransferDetail { 11 public int StartPosition { get; set; } 12 public int BytesToRead { get; set; } 13 public string BlockId { get; set; } 14 } 15 5 16 //check if a blob exists 6 17 // see: http://stackoverflow.com/questions/2642919/checking-if-a-blob-exists-in-azure-storage … … 17 28 } 18 29 } 30 } 31 32 public static void UploadParallel(this CloudBlockBlob blob, string filePath) { 33 FileInfo file = new FileInfo(filePath); 34 //CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.Name); 35 blob.Properties.ContentType = "application/octet-stream"; 36 37 // init 38 int blockSize = 262144; 39 long fileSize = file.Length; 40 long leftToRead = fileSize; 41 int startPosition = 0; 42 43 int blockCount = ((int)Math.Floor((double)(fileSize / blockSize))) + 1; 44 BlockTransferDetail[] transferDetails = new BlockTransferDetail[blockCount]; 45 List<string> blockIdList = new List<string>(); 46 47 // setup control array 48 for (int j = 0; j < transferDetails.Length; j++) { 49 int toRead = (int)(leftToRead > blockSize ? blockSize : leftToRead); 50 string blockId = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}", j.ToString("0000000")))); 51 transferDetails[j] = new BlockTransferDetail() { StartPosition = startPosition, BytesToRead = toRead, BlockId = blockId }; 52 if (toRead > 0) { 53 blockIdList.Add(blockId); 54 } 55 startPosition += toRead; 56 leftToRead -= toRead; 57 } 58 59 // perform parallel upload 60 var result = Parallel.For(0, transferDetails.Length, j => { 61 using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { 62 byte[] buffer = new byte[transferDetails[j].BytesToRead]; 63 BinaryReader br = new BinaryReader(fs); 64 fs.Seek(transferDetails[j].StartPosition, SeekOrigin.Begin); 65 br.Read(buffer, 0, transferDetails[j].BytesToRead); 66 if (buffer.Length > 0) { 67 using (MemoryStream ms = new MemoryStream(buffer)) { 68 blob.PutBlock(transferDetails[j].BlockId, ms, null); 69 } 70 } 71 } 72 }); 73 blob.PutBlockList(blockIdList); 19 74 } 20 75
Note: See TracChangeset
for help on using the changeset viewer.