【发布时间】:2012-07-28 22:16:51
【问题描述】:
我在 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 分钟内被 bean 上传,然后速度下降。 我认为这可能是由于文件名是 GUID 并且 Azure 尝试构建聚集索引。 如何加快速度?有什么问题?
【问题讨论】:
-
GetFiles返回字符串,对吗?但是StoreFile需要一个流......我错过了什么? (我想知道你在哪里处理流。也许有什么东西泄漏了。)你可能只想做for (int i = 0; i < 100000; i++) { container.GetBlobReference(Guid.NewGuid().ToString()).UploadText(i.ToString()); }来简化你正在测量的东西。 -
我不认为 memleaks 或处理对上传速度有影响。通过调用 File.OpenRead(filename) 创建流
-
这些流是否妥善处理?
-
根据您上面的回答,您是否在以下代码行中从文件变量创建流: storage.StoreFile(file, id, ((FileType)comboBoxManyUploadTypes.SelectedItem).Number);这一行其实就是 storage.StoreFile(File.OpenRead(file), id, ((FileType)comboBoxManyUploadTypes.SelectedItem).Number);而不是 storage.StoreFile(stream, id, ((FileType)comboBoxManyUploadTypes.SelectedItem).Number);在你的代码中。对吗?
-
@smarx 我已经更新了问题。
标签: c# .net azure azure-blob-storage