Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A test console app to upload multiple blobs 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;DevelopmentStorageProxyUri=http://ipv4.fiddler");
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(20))
{
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