protected void SaveVhdAndAssertContent(BlobHandle destination, FileInfo localFile, int? numThread, string storageKey, bool overwrite, bool deleteBlob, bool deleteLocal)
        {
            try
            {
                Console.WriteLine("Downloading a VHD from {0} to {1}...", destination.Blob.Uri.ToString(), localFile.FullName);
                DateTime startTime = DateTime.Now;
                VhdDownloadContext result = vmPowershellCmdlets.SaveAzureVhd(destination.Blob.Uri, localFile, numThread, storageKey, overwrite);
                Console.WriteLine("Downloading completed in {0} seconds.", (DateTime.Now - startTime).TotalSeconds);

                string calculateMd5Hash = CalculateContentMd5(File.OpenRead(result.LocalFilePath.FullName));

                Assert.IsTrue(VerifyMD5hash(destination, calculateMd5Hash));

                if (deleteBlob)
                {
                    destination.Blob.Delete();
                }

                if (deleteLocalFileIfPassed && deleteLocal)
                {
                    File.Delete(localFile.FullName);
                }
            }
            catch (Exception e)
            {
                if (deleteLocalFileIfFailed && deleteLocal)
                {
                    File.Delete(localFile.FullName);
                }
                Assert.Fail(e.InnerException.ToString());
            }
        }
        public void Initialize()
        {
            pass = true;
            testStartTime = DateTime.Now;
            storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(defaultAzureSubscription.CurrentStorageAccountName);  

            // Set the source blob
            blobHandle = Utilities.GetBlobHandle(vhdBlobLocation, storageAccountKey.Primary);            
        }
        public void Download()
        {
            if (parameters.OverWrite)
            {
                DeleteTempVhdIfExist(parameters.LocalFilePath);
            }
            else
            {
                if (File.Exists(parameters.LocalFilePath))
                {
                    var message = String.Format("File already exists, you can use Overwrite option to delete it:'{0}'", parameters.LocalFilePath);
                    throw new ArgumentException(message);
                }
            }

            var blobHandle = new BlobHandle(parameters.BlobUri, this.parameters.StorageAccountKey);

            if (parameters.ValidateFreeDiskSpace)
            {
                TryValidateFreeDiskSpace(parameters.LocalFilePath, blobHandle.Length);
            }

            const int megaByte = 1024 * 1024;

            var ranges         = blobHandle.GetUploadableRanges();
            var bufferManager  = BufferManager.CreateBufferManager(Int32.MaxValue, 20 * megaByte);
            var downloadStatus = new ProgressStatus(0, ranges.Sum(r => r.Length), new ComputeStats());

            Trace.WriteLine(String.Format("Total Data:{0}", ranges.Sum(r => r.Length)));

            Program.SyncOutput.WriteVerboseWithTimestamp("Downloading the blob: {0}", parameters.BlobUri.BlobName);

            var fileStreamLock = new object();

            using (new ServicePointHandler(parameters.BlobUri.Uri, parameters.ConnectionLimit))
            {
                using (ProgressTracker progressTracker = new ProgressTracker(downloadStatus, parameters.ProgressDownloadStatus, parameters.ProgressDownloadComplete))
                {
                    using (var fileStream = new FileStream(parameters.LocalFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, 8 * megaByte, FileOptions.WriteThrough))
                    {
                        fileStream.SetLength(0);
                        fileStream.SetLength(blobHandle.Length);

                        Task <LoopResult> task = Task <LoopResult> .Factory.StartNew(() =>
                        {
                            return(Threading.Parallel.ForEach <IndexRange, Stream>(ranges,
                                                                                   blobHandle.OpenStream,
                                                                                   (r, b) =>
                            {
                                b.Seek(r.StartIndex, SeekOrigin.Begin);

                                byte[] buffer = this.EnsureReadAsSize(b, (int)r.Length, bufferManager);

                                lock (fileStreamLock)
                                {
                                    Trace.WriteLine(String.Format("Range:{0}", r));
                                    fileStream.Seek(r.StartIndex, SeekOrigin.Begin);
                                    fileStream.Write(buffer, 0, (int)r.Length);
                                    fileStream.Flush();
                                }

                                downloadStatus.AddToProcessedBytes((int)r.Length);
                            },
                                                                                   pbwlf =>
                            {
                                pbwlf.Dispose();
                            },
                                                                                   parameters.ConnectionLimit));
                        });

                        while (!task.Wait(TimeSpan.FromSeconds(1)))
                        {
                            progressTracker.Update();
                        }

                        LoopResult lr = task.Result;

                        if (lr.IsExceptional)
                        {
                            throw new AggregateException(lr.Exceptions);
                        }
                    }
                }
            }
            Program.SyncOutput.WriteVerboseWithTimestamp("Blob downloaded successfullty: {0}", parameters.BlobUri.BlobName);
        }
 protected static void SaveVhd(BlobHandle destination, FileInfo locFile, string storageKey, int? numThread = null, bool overwrite = false)
 {
     try
     {
         Console.WriteLine("Downloading a VHD from {0} to {1}...", destination.Blob.Uri.ToString(), locFile.FullName);
         DateTime startTime = DateTime.Now;
         vmPowershellCmdlets.SaveAzureVhd(destination.Blob.Uri, locFile, numThread, storageKey, overwrite);
         Console.WriteLine("Downloading completed in {0} seconds.", (DateTime.Now - startTime).TotalSeconds);
     }
     catch (Exception e)
     {
         Assert.Fail(e.InnerException.ToString());
     }
 }
Example #5
0
        public void Download()
        {
            if (parameters.OverWrite)
            {
                DeleteTempVhdIfExist(parameters.LocalFilePath);
            }
            else
            {
                if (File.Exists(parameters.LocalFilePath))
                {
                    var message = String.Format("File already exists, you can use Overwrite option to delete it:'{0}'", parameters.LocalFilePath);
                    throw new ArgumentException(message);
                }
            }

            var blobHandle = new BlobHandle(parameters.BlobUri, this.parameters.StorageAccountKey);

            if (parameters.ValidateFreeDiskSpace)
            {
                TryValidateFreeDiskSpace(parameters.LocalFilePath, blobHandle.Length);
            }

            const int megaByte = 1024 * 1024;

            var ranges = blobHandle.GetUploadableRanges();
            var bufferManager = BufferManager.CreateBufferManager(Int32.MaxValue, 20 * megaByte);
            var downloadStatus = new ProgressStatus(0, ranges.Sum(r => r.Length), new ComputeStats());

            Trace.WriteLine(String.Format("Total Data:{0}", ranges.Sum(r => r.Length)));

            Program.SyncOutput.WriteVerboseWithTimestamp("Downloading the blob: {0}", parameters.BlobUri.BlobName);

            var fileStreamLock = new object();
            using (new ServicePointHandler(parameters.BlobUri.Uri, parameters.ConnectionLimit))
            {
                using (new ProgressTracker(downloadStatus, parameters.ProgressDownloadStatus, parameters.ProgressDownloadComplete, TimeSpan.FromSeconds(1)))
                {
                    using (var fileStream = new FileStream(parameters.LocalFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, 8 * megaByte, FileOptions.WriteThrough))
                    {
                        fileStream.SetLength(0);
                        fileStream.SetLength(blobHandle.Length);

                        LoopResult lr = Parallel.ForEach<IndexRange, Stream>(ranges,
                                    blobHandle.OpenStream,
                                    (r, b) =>
                                        {
                                            b.Seek(r.StartIndex, SeekOrigin.Begin);

                                            byte[] buffer = this.EnsureReadAsSize(b, (int)r.Length, bufferManager);

                                            lock (fileStreamLock)
                                            {
                                                Trace.WriteLine(String.Format("Range:{0}", r));
                                                fileStream.Seek(r.StartIndex, SeekOrigin.Begin);
                                                fileStream.Write(buffer, 0, (int)r.Length);
                                                fileStream.Flush();
                                            }

                                            downloadStatus.AddToProcessedBytes((int)r.Length);
                                        },
                                    pbwlf =>
                                        {
                                            pbwlf.Dispose();
                                        },
                                    parameters.ConnectionLimit);

                        if (lr.IsExceptional)
                        {
                            throw new AggregateException(lr.Exceptions);
                        }
                    }
                }
            }
            Program.SyncOutput.WriteVerboseWithTimestamp("Blob downloaded successfullty: {0}", parameters.BlobUri.BlobName);
        }
 protected bool VerifyMD5hash(BlobHandle blobHandle, string md5hash)
 {
     string blobMd5 = blobHandle.Blob.Properties.ContentMD5;
     Console.WriteLine("MD5 hash of the local file: {0}", md5hash);
     if (string.IsNullOrEmpty(blobMd5))
     {
         Console.WriteLine("The blob does not have MD5 value!!!");
         return false;
     }
     else
     {
         Console.WriteLine("MD5 hash of blob, {0}, is {1}.", blobHandle.Blob.Uri.ToString(), blobMd5);
         return String.Equals(blobMd5, md5hash);
     }
 }
 protected void SaveVhdAndAssertContent(BlobHandle destination, FileInfo localFile, bool overwrite, bool deleteBlob, bool deleteLocal)
 {
     SaveVhdAndAssertContent(destination, localFile, null, null, overwrite, deleteBlob, deleteLocal);
 }
 protected void SaveVhdAndAssertContent(BlobHandle destination, FileInfo localFile, string storageKey, bool deleteBlob, bool deleteLocal)
 {
     SaveVhdAndAssertContent(destination, localFile, null, storageKey, false, deleteBlob, deleteLocal);
 }
 protected void SaveVhdAndAssertContent(BlobHandle destination, FileInfo localFile, int? numThread, bool deleteBlob, bool deleteLocal)
 {
     SaveVhdAndAssertContent(destination, localFile, numThread, null, false, deleteBlob, deleteLocal);
 }