批量上传文件Azure Blob Storage的最快方法是什么?我已经尝试了两种方法,sync
和async
上传,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, randomData, 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, 0, data.Length);
}
Task.WaitAll(tasks);
sw.Stop();
return sw.Elapsed;
}
public static TimeSpan Test1(int total, byte[] data, 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, 0, data.Length);
}
sw.Stop();
return sw.Elapsed;
}
这个输出是:
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
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
我希望这是一个有效的问题。
由于
编辑:
我用&#34; DMLib&#34;更新了结果。针对目前给出的答案进行测试。 DMLib是一个没有配置更改的测试(见上文)没有性能提升
我按照文档的推荐用ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;
运行了一些测试,这大大提高了上传速度,但它也增加了我的异步方法的上传速度。到目前为止,DMlib还没有给我任何值得的性能提升。我在下面的配置更改中添加了第二组测试结果。
我也设置ServicePointManager.Expect100Continue = false;
,但这对速度没有影响。
ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;
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
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
我是否错误地使用了库,因为它似乎没有提供比我自己的方法更好的性能。
答案 0 :(得分:6)
请使用Azure Storage Data Movement Library,这是AzCopy的核心。该库正是解决问题的工具。 :)
答案 1 :(得分:1)
使用Azcopy完成工作。不幸的是,它是一个独立的exe。
您还可以将文件拆分为块(按开始和偏移读取)并并行上传。这有点复杂,你必须根据机器调整上传线程。