// Get a page of a page blob. // Return true on success, false if unable to create, throw exception on error. public bool GetPage(string containerName, string blobName, int pageOffset, int pageSize, out string content) { content = null; try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); CloudPageBlob blob = container.GetPageBlobReference(blobName); BlobStream stream = blob.OpenRead(); byte[] data = new byte[pageSize]; stream.Seek(pageOffset, SeekOrigin.Begin); stream.Read(data, 0, pageSize); content = new UTF8Encoding().GetString(data); stream.Close(); return(true); } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return(false); } throw; } }
private static void DownloadVHDFromCloud(Config config) { StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey(config.Account, config.Key); CloudBlobClient blobStorage = new CloudBlobClient(config.AccountUrl, creds); blobStorage.ReadAheadInBytes = 0; CloudBlobContainer container = blobStorage.GetContainerReference(config.Container); CloudPageBlob pageBlob = container.GetPageBlobReference(config.Blob); // Get the length of the blob pageBlob.FetchAttributes(); long vhdLength = pageBlob.Properties.Length; long totalDownloaded = 0; Console.WriteLine("Vhd size: " + Megabytes(vhdLength)); // Create a new local file to write into FileStream fileStream = new FileStream(config.Vhd.FullName, FileMode.Create, FileAccess.Write); fileStream.SetLength(vhdLength); // Download the valid ranges of the blob, and write them to the file IEnumerable <PageRange> pageRanges = pageBlob.GetPageRanges(); BlobStream blobStream = pageBlob.OpenRead(); foreach (PageRange range in pageRanges) { // EndOffset is inclusive... so need to add 1 int rangeSize = (int)(range.EndOffset + 1 - range.StartOffset); // Chop range into 4MB chucks, if needed for (int subOffset = 0; subOffset < rangeSize; subOffset += FourMegabyteAsBytes) { int subRangeSize = Math.Min(rangeSize - subOffset, FourMegabyteAsBytes); blobStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); fileStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); Console.WriteLine("Range: ~" + Megabytes(range.StartOffset + subOffset) + " + " + PrintSize(subRangeSize)); byte[] buffer = new byte[subRangeSize]; blobStream.Read(buffer, 0, subRangeSize); fileStream.Write(buffer, 0, subRangeSize); totalDownloaded += subRangeSize; } } Console.WriteLine("Downloaded " + Megabytes(totalDownloaded) + " of " + Megabytes(vhdLength)); }
public byte[] DownLoadBlock(string fileName, long offSet, int blockSize, int userId) { var blob = GetBlockBlob(fileName, userId); if (blob.Exists()) { BlobStream reader = blob.OpenRead(); reader.Seek(offSet, SeekOrigin.Begin); byte[] bufferBytes = new byte[blockSize]; int total = reader.Read(bufferBytes, 0, blockSize); return(bufferBytes); } return(null); }
public static void ParallelDownloadToFile(CloudBlockBlob blob, string fileName, int maxBlockSize) { try { // refresh the values blob.FetchAttributes(); long fileSize = blob.Attributes.Properties.Length; var filePath = Path.GetDirectoryName(fileName); var fileNameWithoutPath = Path.GetFileNameWithoutExtension(fileName); // let's figure out how big the file is here long leftToRead = fileSize; int startPosition = 0; // have 1 block for every maxBlockSize bytes plus 1 for the remainder var blockCount = ((int)Math.Floor((double)(fileSize / maxBlockSize))) + 1; // setup the control array BlockTransferDetail[] transferDetails = new BlockTransferDetail[blockCount]; // create an array of block keys string[] blockKeys = new string[blockCount]; var blockIds = new List <string>(); // populate the control array... for (int j = 0; j < transferDetails.Length; j++) { int toRead = (int)(maxBlockSize < leftToRead ? maxBlockSize : leftToRead); string blockId = Path.Combine(filePath, string.Format("{0}_{1}.dat", fileNameWithoutPath, j.ToString("00000000000"))); if (startPosition < 0) { startPosition = startPosition * -1; } if (toRead < 0) { toRead = toRead * -1; } transferDetails[j] = new BlockTransferDetail() { StartPosition = startPosition, BytesToRead = toRead, BlockId = blockId }; if (toRead > 0) { blockIds.Add(blockId); } // increment the starting position startPosition += toRead; leftToRead -= toRead; } // now we do a || download of the file. var result = Parallel.For(0, transferDetails.Length, j => { // get the blob as a stream try { using (BlobStream stream = blob.OpenRead()) { Thread.Sleep(10000); stream.Seek(transferDetails[j].StartPosition, SeekOrigin.Begin); // setup a buffer with the proper size byte[] buff = new byte[transferDetails[j].BytesToRead]; // read into the buffer stream.Read(buff, 0, transferDetails[j].BytesToRead); using (Stream fileStream = new FileStream(transferDetails[j].BlockId, FileMode.Create, FileAccess.Write, FileShare.None)) { using (BinaryWriter bw = new BinaryWriter(fileStream)) { bw.Write(buff); bw.Close(); } } buff = null; } } catch (Exception) { throw; } }); // assemble the file into one now... using (Stream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None)) { using (BinaryWriter bw = new BinaryWriter(fileStream)) { // loop through each of the files on the disk for (int j = 0; j < transferDetails.Length; j++) { // read them into the file (append) bw.Write(System.IO.File.ReadAllBytes(transferDetails[j].BlockId)); // and then delete them System.IO.File.Delete(transferDetails[j].BlockId); } } } transferDetails = null; } catch (Exception) { throw; } }