Ejemplo n.º 1
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);

            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 (fileStream)
                                            {
                                                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);
        }
Ejemplo n.º 2
0
        public void Download(string destination)
        {
            DeleteTempVhdIfExist(destination);

            Console.WriteLine("\t\tDownloading blob '{0}' ...", blobUri.BlobName);
            Console.WriteLine("\t\tImage download start time: '{0}'", DateTime.UtcNow.ToString("o"));

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

            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)));

            const int maxParallelism = 24;
            using (new ServicePointHandler(this.blobUri.Uri, maxParallelism))
            using (new ProgressTracker(downloadStatus, Program.SyncOutput.ProgressUploadStatus, Program.SyncOutput.ProgressUploadComplete, TimeSpan.FromSeconds(1)))
            {
            //                if(SparseFile.VolumeSupportsSparseFiles(destination))
            //                {
            //                   using(var fileStream = SparseFile.Create(destination))
            //                   {
            //                       foreach (var emptyRange in blobHandle.GetEmptyRanges())
            //                       {
            //                           SparseFile.SetSparseRange(fileStream.SafeFileHandle, emptyRange.StartIndex, emptyRange.Length);
            //                       }
            //                   }
            //                }

                using (var fileStream = new FileStream(destination, 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 (fileStream)
                                         {
                                             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();
                                     },
                                     maxParallelism);
                    if (lr.IsExceptional)
                    {
                        Console.WriteLine("\t\tException(s) happened");
                        for (int i = 0; i < lr.Exceptions.Count; i++)
                        {
                            Console.WriteLine("{0} -> {1}", i, lr.Exceptions[i]);
                        }
                    }
                }
            }

            Console.WriteLine("\t\tImage download end time  : '{0}'", DateTime.UtcNow.ToString("o"));
        }
Ejemplo n.º 3
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);
        }