2012-07-31 142 views
1

我有Azure blob存储中的试用帐户。我尝试从本地机器上传100000个生成的文件。该操作已经有超过17个小时的时间,并且只上传〜77000个文件。 通过一个简单的bash脚本创建的所有文件:Azure blob存储 - 上传速度非常慢

for i in {1..100000} 
do 
    echo $i 
    echo $i > $1\\$i.txt 
done 

代码上传:

using(var stream = File.OpenWrite(textBoxManyUploadFileName.Text)) 
using(var writer = new StreamWriter(stream)) { 
    foreach(var file in Directory.GetFiles(textBoxManyUploadFrom.Text)) { 
     Guid id = Guid.NewGuid(); 
     storage.StoreFile(file, id, ((FileType)comboBoxManyUploadTypes.SelectedItem).Number); 
     writer.WriteLine("{0}={1}", id, file); 
    } 
} 

public void StoreFile(Stream stream, Guid id, string container) { 
    try { 
     var blob = GetBlob(id, container); 
     blob.UploadFromStream(stream); 
    } catch(StorageException exception) { 
     throw TranslateException(exception, id, container); 
    } 
} 

public void StoreFile(string filename, Guid id, int type = 0) { 
    using(var stream = File.OpenRead(filename)) { 
     StoreFile(stream, id, type); 
    } 
} 

CloudBlob GetBlob(Guid id, string containerName) { 
    var container = azureBlobClient.GetContainerReference(containerName); 
    if(container.CreateIfNotExist()) { 
     container.SetPermissions(new BlobContainerPermissions { 
      PublicAccess = BlobContainerPublicAccessType.Container 
     }); 
    } 
    return container.GetBlobReference(id.ToString()); 
} 

第10000个文件有豆20-30分钟,然后上传的速度下降。 我认为这可能是由于文件名是GUID并且Azure试图构建聚集索引。 如何加快速度?问题是什么?

+0

'GetFiles'返回字符串,对吧?但'StoreFile'需要一个流...我错过了什么? (我想知道你在哪里处理这个流,也许有些东西在泄漏)。你可能想要做'for(int i = 0; i <100000; i ++){container.GetBlobReference(Guid.NewGuid()。ToString ())UploadText(i.ToString())。 }来简化你正在测量的内容。 – smarx 2012-07-31 08:07:59

+0

我不认为这些memleaks或处理上传速度的影响。流是通过调用File.OpenRead(filename) – brainstream 2012-07-31 08:24:11

+0

创建的,那些流是否正确放置? – smarx 2012-07-31 08:52:59

回答

2

要上传很多小文件,您应该使用多个线程。 例如,您可以使用BeginUploadFromStreamParallel.ForEach

+0

为什么前10000个文件上传得很快? – brainstream 2012-07-31 07:53:04

+0

使用分析器查找瓶颈。它可能是你的日志:writer.WriteLine(“{0} = {1}”,id,file); 你还使用最新的SDK? – Guillaume 2012-07-31 08:41:55

+0

谢谢你的建议。我曾尝试使用本地文件保存功能,并且它们已在~20分钟内写入。所以,问题不在我的代码中。是的,我正在使用最新的SDK。 – brainstream 2012-07-31 09:13:58

1

我在代码中注意到的另一件事是,您在StoreFile()函数中调用GetBlob()函数,该函数又会在您的blob容器上调用CreateIfNotExist()函数。请注意,此功能还会导致对Storage Service的调用,从而延长上传过程的时间(更不用说每次调用此功能时还要为存储事务收费)。

我建议您在开始blob上传之前调用此函数一次。

+0

对于第一个10000个文件,CreateIfNotExist()没有扮演任何角色。 – brainstream 2012-07-31 07:50:04