批量上传文件
Azure Blob Storage的最快方法是什么?我尝试了两种方法,同步和异步上传,async显然是最快的,但我想知道是否有更好的方法?是否内置了对批量上传的支持?我在文档中找不到任何内容但可能错过了它.
这是我跑的测试:
static void Main(string[] args) { int totalFiles = 10; //10,50,100 byte[] randomData = new byte[2097152]; //2mb for (int i = 0; i < randomData.Length; i++) { randomData[i] = 255; } CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); var blobClient = cloudStorageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference("something"); container.CreateIfNotExists(); TimeSpan tsSync = Test1(totalFiles,randomData,container); TimeSpan tsAsync = Test2(totalFiles,container); Console.WriteLine($"Sync: {tsSync}"); Console.WriteLine($"Async: {tsAsync}"); Console.ReadLine(); } public static TimeSpan Test2(int total,byte[] data,CloudBlobContainer container) { Stopwatch sw = new Stopwatch(); sw.Start(); Task[] tasks = new Task[total]; for (int i = 0; i < total; i++) { CloudBlockBlob blob = container.GetBlockBlobReference(Guid.NewGuid().ToString()); tasks[i] = blob.UploadFromByteArrayAsync(data,data.Length); } Task.WaitAll(tasks); sw.Stop(); return sw.Elapsed; } public static TimeSpan Test1(int total,CloudBlobContainer container) { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < total; i++) { CloudBlockBlob blob = container.GetBlockBlobReference(Guid.NewGuid().ToString()); blob.UploadFromByteArray(data,data.Length); } sw.Stop(); return sw.Elapsed; }
这个输出是:
10个文件
Sync: 00:00:08.7251781 Async: 00:00:04.7553491 DMLib: 00:00:05.1961654 Sync: 00:00:08.1169861 Async: 00:00:05.2384105 DMLib: 00:00:05.4955403 Sync: 00:00:07.6122464 Async: 00:00:05.0495365 DMLib: 00:00:06.4714047
50个文件
Sync: 00:00:39.1595797 Async: 00:00:22.5757347 DMLib: 00:00:25.2897623 Sync: 00:00:40.4932800 Async: 00:00:22.3296490 DMLib: 00:00:26.0631829 Sync: 00:00:39.2879245 Async: 00:00:24.0746697 DMLib: 00:00:26.9243116
我希望这是一个有效的问题.
谢谢
编辑:
我已经用“DMLib”测试更新了结果,以回应到目前为止给出的答案. DMLib是一个没有配置更改的测试(见上文)没有性能提升
我用ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8运行了一些测试;正如文档所推荐的那样,这增加了相当多的上传速度,但它也提高了我的异步方法的上传速度.到目前为止,DMlib还没有给我任何值得的性能提升.我在下面的配置更改中添加了第二组测试结果.
我还设置了ServicePointManager.Expect100Continue = false;然而这对速度没有影响.
使用ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8测试结果;
10个文件
Sync: 00:00:07.6199307 Async: 00:00:02.9615565 DMLib: 00:00:02.6629716 Sync: 00:00:08.7721797 Async: 00:00:02.8246599 DMLib: 00:00:02.7281091 Sync: 00:00:07.8437682 Async: 00:00:03.0171246 DMLib: 00:00:03.0190045
50个文件
Sync: 00:00:40.2395863 Async: 00:00:10.3157544 DMLib: 00:00:10.5107740 Sync: 00:00:40.2473358 Async: 00:00:10.8190161 DMLib: 00:00:10.2585441 Sync: 00:00:41.2646137 Async: 00:00:13.7188085 DMLib: 00:00:10.8686173
解决方法
请使用
Azure Storage Data Movement Library,这是
AzCopy的核心.这个库正是解决问题的工具. 原文链接:https://www.f2er.com/csharp/244957.html