Created
February 6, 2016 14:55
-
-
Save tuan/bb77553f540ae45d2326 to your computer and use it in GitHub Desktop.
Upload multiple files to azure blob storage in parallel
This file contains 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"); | |
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