簡體   English   中英

Azure blob存儲 - 上傳速度非常慢

[英]Azure blob storage - uploading very slow

我在azure blob存儲中有試用帳戶。 我嘗試從本地計算機上傳100000個生成的文件。 該操作已持續超過17個小時,僅上傳了約77000個文件。 由簡單的bash腳本創建的所有文件:

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嘗試構建聚簇索引。 怎么加快? 問題是什么?

要上傳許多小文件,您應該使用多個線程。 例如,您可以使用BeginUploadFromStreamParallel.ForEach

我在你的代碼中注意到的另一件事是你在StoreFile()函數中調用GetBlob()函數,該函數又調用blob容器上的CreateIfNotExist()函數。 請注意,此功能還會導致對存儲服務的調用,從而增加上傳過程的延遲(更不用說每次調用此功能時,您還需要為存儲事務付費)。

我建議你在開始blob上傳之前只調用一次這個函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM