Skip to content

Instantly share code, notes, and snippets.

@0GiS0
Last active February 3, 2019 19:55
Show Gist options
  • Save 0GiS0/fbd15147847b4ee6aeadc9fd060525db to your computer and use it in GitHub Desktop.
Save 0GiS0/fbd15147847b4ee6aeadc9fd060525db to your computer and use it in GitHub Desktop.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Core.Util;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AzureBlobAsyncProgress
{
class Program
{
static long totalBytes = 0;
static void Main(string[] args)
{
UploadBlobAsync().GetAwaiter().GetResult();
Console.WriteLine("Blob uploaded");
Console.ReadLine();
}
static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
private static async Task UploadBlobAsync()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("YOUR_STORAGE_ACCOUNT_CONNECTION_STRING");
var client = storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference("wavs");
await container.CreateIfNotExistsAsync();
var file = File.Open("audio.wav", FileMode.Open);
totalBytes = file.Length;
var blob = container.GetBlockBlobReference(file.Name);
var progressHandler = new Progress& amp; amp; amp; lt; StorageProgress & amp; amp; amp; gt; ();
progressHandler.ProgressChanged += ProgressHandler_ProgressChanged;
//await blob.UploadFromStreamAsync(file);
// UploadFromStreamAsync(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, IProgress<StorageProgress> progressHandler, CancellationToken cancellationToken);
await blob.UploadFromStreamAsync(file, null, new BlobRequestOptions(), new OperationContext(), progressHandler, CancellationToken.None);
}
private static void ProgressHandler_ProgressChanged(object sender, StorageProgress e)
{
double dProgress = ((double)e.BytesTransferred / totalBytes) * 100.0;
Console.WriteLine($"{dProgress.ToString("0.00")}% Bytes transferred: { ConvertBytesToMegabytes(e.BytesTransferred).ToString("0.00")}MB from {ConvertBytesToMegabytes(totalBytes).ToString("0.00")}MB");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment