Created
February 6, 2016 22:28
-
-
Save tuan/e06951f048b194cb2ceb to your computer and use it in GitHub Desktop.
A test console app to upload multiple blobs to Azure Blob Storage in parallel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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