Skip to content

Instantly share code, notes, and snippets.

@tuan
Created February 6, 2016 14:55
Show Gist options
  • Save tuan/bb77553f540ae45d2326 to your computer and use it in GitHub Desktop.
Save tuan/bb77553f540ae45d2326 to your computer and use it in GitHub Desktop.
Upload multiple files to azure blob storage in parallel
class Program
{
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
UploadToBlobAsync().Wait();
stopwatch.Stop();
Debug.WriteLine("Elapsed time in seconds: " + stopwatch.ElapsedMilliseconds / 1000);
}
private static async Task UploadToBlobAsync()
{
var storageAccount = CloudStorageAccount.Parse(@"UseDevelopmentStorage=true");
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container" + Guid.NewGuid().ToString("N").ToLowerInvariant());
await container.CreateIfNotExistsAsync().ConfigureAwait(false);
var baseDir = @"E:\Source\Ghost\core";
var files = Directory.GetFiles(baseDir, "*", SearchOption.AllDirectories);
using (var semaphoreSlim = new SemaphoreSlim(2))
{
var tasks = files.Select(async file =>
{
await semaphoreSlim.WaitAsync().ConfigureAwait(false);
try
{
var blobName = ConvertToRelativeUri(file, baseDir);
var blob = container.GetBlockBlobReference(blobName);
using (var fileStream = File.OpenRead(file))
{
await blob.UploadFromStreamAsync(fileStream).ConfigureAwait(false);
}
}
finally
{
semaphoreSlim.Release();
}
});
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
private static string ConvertToRelativeUri(string filePath, string baseDir)
{
var uri = new Uri(filePath);
if (!baseDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
baseDir += Path.DirectorySeparatorChar.ToString();
}
var baseUri = new Uri(baseDir);
return baseUri.MakeRelativeUri(uri).ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment