示例#1
0
        public void DownloadRangeToStream(Stream stream, long offset, int length)
        {
            Require.NotNull("stream", stream);
            Require.ZeroOrGreater("offset", offset);
            Require.Positive("length", length);

            try {
                _blob.DownloadRangeToStream(stream, offset, length);
            }
            catch (StorageException ex) {
                // if forbidden, then we might have an expired SAS token
                if (ex.RequestInformation != null && ex.RequestInformation.HttpStatusCode == 403)
                {
                    throw new ForbiddenException("Can't read blob", ex);
                }
                throw;
            }
        }
示例#2
0
        public void DownloadRangeToStream(Stream stream, long offset, int length)
        {
            Require.NotNull("stream", stream);
            Require.ZeroOrGreater("offset", offset);
            Require.Positive("length", length);

            try {
                var context = new OperationContext();
                context.SendingRequest += (sender, e) => {
                    e.Request.Headers["if-match"] = "*";
                };
                _blob.DownloadRangeToStream(stream, offset, length, null, null, context);
            }
            catch (StorageException ex) {
                // if forbidden, then we might have an expired SAS token
                if (ex.RequestInformation != null && ex.RequestInformation.HttpStatusCode == 403)
                {
                    throw new ForbiddenException("Can't read blob", ex);
                }
                throw;
            }
        }
示例#3
0
        private static void Download(string accountName, string accountKey, string containerName, string blobName, string localFilePath, int segmentSize, int delayBeetweenChunksInSeconds)
        {
            var           cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accountKey);
            var           blobContainer       = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName);
            CloudPageBlob blob = blobContainer.GetPageBlobReference(blobName);

            blob.FetchAttributes();

            long startPosition = 0;

            if (File.Exists(localFilePath))
            {
                log.AddLine("Local file exists. Resuming download...");
                using (FileStream fs = new FileStream(localFilePath, FileMode.Open))
                {
                    fs.Seek(0, SeekOrigin.End);
                    startPosition = fs.Position;
                }
            }

            long blobLength          = blob.Properties.Length;
            var  blobLengthRemaining = blobLength - startPosition;

            DateTime momentBeforeRead = DateTime.Now;

            double   percentDone         = 0;
            TimeSpan timeForOneRead      = new TimeSpan(0);
            double   speedBytesPerSecond = 0;
            TimeSpan timeRemaining       = new TimeSpan(0);
            int      secondsRemaining    = 0;

            timeoutTimer = new Timer(Timer_Tick, null, Timeout.Infinite, Timeout.Infinite);
            while (blobLengthRemaining > 0)
            {
                long   blockSize    = Math.Min(segmentSize, blobLengthRemaining);
                byte[] blobContents = new byte[blockSize];
                using (MemoryStream ms = new MemoryStream())
                {
                    timeoutTimer.Change(new TimeSpan(0, 0, 10), new TimeSpan(0, 0, 10));
                    blob.DownloadRangeToStream(ms, startPosition, blockSize);
                    timeoutTimer.Change(Timeout.Infinite, Timeout.Infinite);

                    ms.Position = 0;
                    ms.Read(blobContents, 0, blobContents.Length);
                    using (FileStream fs = new FileStream(localFilePath, FileMode.OpenOrCreate))
                    {
                        fs.Position = startPosition;
                        fs.Write(blobContents, 0, blobContents.Length);
                    }
                }
                startPosition       += blockSize;
                blobLengthRemaining -= blockSize;

                //blobLength:100=startPosition:x
                percentDone         = startPosition * 100 / blobLength;
                timeForOneRead      = DateTime.Now.Subtract(momentBeforeRead);
                momentBeforeRead    = DateTime.Now;
                speedBytesPerSecond = blockSize / timeForOneRead.TotalSeconds;
                secondsRemaining    = (int)Math.Round(blobLengthRemaining / speedBytesPerSecond);
                timeRemaining       = new TimeSpan(0, 0, secondsRemaining);

                Console.SetCursorPosition(0, 19);
                Console.WriteLine("                                                                                                                                                             ");
                Console.SetCursorPosition(0, 19);
                log.AddLine(string.Format("{0}%, {1}/{2} bytes, {3} kbytes/sec, remaining (dd:hh:mm:ss): {4}:{5}:{6}:{7}",
                                          percentDone,
                                          startPosition,
                                          blobLength,
                                          Math.Round(speedBytesPerSecond / 1024, 2),
                                          timeRemaining.Days,
                                          timeRemaining.Hours,
                                          timeRemaining.Minutes,
                                          timeRemaining.Seconds
                                          )
                            );


                attempt = 1;

                if (delayBeetweenChunksInSeconds > 0)
                {
                    Thread.Sleep(delayBeetweenChunksInSeconds);
                }
            }
        }