override protected IEnumerator _DoTest() { yield return(base._DoTest()); Start(); Manifest metadata1 = new Manifest("https://s3.amazonaws.com/piko_public/Test.png", ManifestFileStream.Flags.None); Manifest metadata2 = new Manifest("https://s3.amazonaws.com/piko_public/Test2.png", ManifestFileStream.Flags.None); Manifest metadata3 = new Manifest("https://s3.amazonaws.com/piko_public/Test3.png", ManifestFileStream.Flags.None); Progress progress = new Progress(); progress.AddDownload(metadata1); progress.AddDownload(metadata2); progress.AddDownload(metadata3); progress.OnPercentChange += progress_OnPercentChange; _Parent._Manager.AddDownload(metadata1); _Parent._Manager.AddDownload(metadata2); _Parent._Manager.AddDownload(metadata3); while (succeed == -1) { yield return(null); } int bytesDownloaded0 = metadata1.BytesDownloaded + metadata2.BytesDownloaded + metadata3.BytesDownloaded; int totalBytesDownloaded0 = metadata1.TotalBytesSize + metadata2.TotalBytesSize + metadata3.TotalBytesSize; Finish(); if (bytesDownloaded0 == progress.DownloadedBytes && totalBytesDownloaded0 == progress.TotalBytes) { Success(); } else { Fail(); } yield return(null); }
// Overload: Override the default file overwrite setting at the class level for this specific file public async Task <string> DownloadStorageBlockBlobSegmentedOperationAsync(string MediaFile, bool overwrite) { try { // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. WriteLine("Opening Blob Container in Azure Storage."); CloudBlobContainer container = blobClient.GetContainerReference(BlobContainerName); try { await container.CreateIfNotExistsAsync(); } catch (StorageException) { WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample."); throw; } // Access a specific blob in the container WriteLine("Get Specific Blob in Container and its size"); CloudBlockBlob blockBlob = container.GetBlockBlobReference(MediaFile); int segmentSize = SegmentSizeKB * 1024; // SegmentSizeKB is set in the inspector chunk if (blockBlob != null) { // Obtain the size of the blob await blockBlob.FetchAttributesAsync(); long blobSize = blockBlob.Properties.Length; long blobLengthRemaining = blobSize; float completion = 0f; long startPosition = 0; WriteLine("3. Blob size (bytes):" + blobLengthRemaining.ToString()); // Download a blob to your file system string path = ""; WriteLine(string.Format("Downloading Blob from {0}, please wait...", blockBlob.Uri.AbsoluteUri)); string fileName = MediaFile; // string.Format("CopyOf{0}", MediaFile); bool fileExists = false; #if WINDOWS_UWP StorageFolder storageFolder = ApplicationData.Current.TemporaryFolder; StorageFile sf; try { CreationCollisionOption collisionoption = (overwrite ? CreationCollisionOption.ReplaceExisting : CreationCollisionOption.FailIfExists); sf = await storageFolder.CreateFileAsync(fileName, collisionoption); fileExists = false; // if the file existed but we were allowed to overwrite it, let's treat it as if it didn't exist } catch (Exception) { // The file already exists and we're not supposed to overwrite it fileExists = true; sf = await storageFolder.GetFileAsync(fileName); // Necessary to avoid a compilation error below } path = sf.Path; #else path = Path.Combine(Application.temporaryCachePath, fileName); fileExists = File.Exists(path); #endif if (fileExists) { if (overwrite) { WriteLine(string.Format("Already exists. Deleting file {0}", fileName)); #if WINDOWS_UWP // Nothing to do here in UWP, we already Replaced it when we created the StorageFile #else File.Delete(path); #endif } else { WriteLine(string.Format("File {0} already exists and overwriting is disabled. Download operation cancelled.", fileName)); return(path); } } ProgressBar.AddDownload(); #if WINDOWS_UWP var fs = await sf.OpenAsync(FileAccessMode.ReadWrite); #else FileStream fs = new FileStream(path, FileMode.OpenOrCreate); #endif // Start the timer to measure performance var sw = Stopwatch.StartNew(); do { long blockSize = Math.Min(segmentSize, blobLengthRemaining); byte[] blobContents = new byte[blockSize]; using (MemoryStream ms = new MemoryStream()) { await blockBlob.DownloadRangeToStreamAsync(ms, (long)startPosition, blockSize); ms.Position = 0; ms.Read(blobContents, 0, blobContents.Length); #if WINDOWS_UWP fs.Seek((ulong)startPosition); await fs.WriteAsync(blobContents.AsBuffer()); #else fs.Position = startPosition; fs.Write(blobContents, 0, blobContents.Length); #endif } completion = (float)startPosition / (float)blobSize; WriteLine("Completed: " + (completion).ToString("P")); ProgressBar.Value = (completion * 100); startPosition += blockSize; blobLengthRemaining -= blockSize; }while (blobLengthRemaining > 0); WriteLine("Completed: 100.00%"); ProgressBar.Value = 100; ProgressBar.RemoveDownload(); #if !WINDOWS_UWP // Required for Mono & .NET or we'll get a file IO access violation the next time we try to access it fs.Close(); #else fs.Dispose(); #endif fs = null; // Stop the timer and report back on completion + performance sw.Stop(); TimeSpan time = sw.Elapsed; WriteLine(string.Format("5. Blob file downloaded to {0} in {1}s", path, time.TotalSeconds.ToString())); return(path); } else { WriteLine(string.Format("3. File {0} not found in blob {1}", MediaFile, blockBlob.Uri.AbsoluteUri)); return(string.Empty); } } catch (Exception ex) { // Woops! WriteLine(string.Format("Error while downloading file {0}", MediaFile)); WriteLine("Error: " + ex.ToString()); WriteLine("Error: " + ex.InnerException.ToString()); return(string.Empty); } }