我如何知道 Azure 文件存储共享何时被删除?
How do I know when an Azure File Storage share has been deleted?
我正在尝试删除并重新创建 Azure 存储文件共享,作为删除全部内容的快速方法。
问题是,当我尝试立即重新创建与之前删除的旧共享同名的新共享时,它失败了(出现 409 冲突)错误。如果我在删除它后等待大约 30 秒,它就可以正常工作。我认为这是因为它需要时间来释放共享名称。
这是我的代码:
var targetAccount = new CloudStorageAccount(new StorageCredentials(destination.StorageAccountName, destination.Key), true);
var targetClient = targetAccount.CreateCloudFileClient();
var targetShare = targetClient.GetShareReference(destination.ShareName);
if (targetShare.Exists()) {
var ar = targetShare.BeginDelete(null, null);
targetShare.EndDelete(ar);
}
Thread.Sleep(30000);
targetShare.Create();
根据 MSDN 上的文档,EndDelete
应该会阻塞直到删除完成,那为什么不呢?
如何避免必须等待一段固定的时间才能完成删除?
(我也试过这个的同步版本,但这完全一样)
更新
尝试了一些不同的东西,我想使用以下代码查看从 BeginDelete 回调的时间:
if (targetShare.Exists()) {
Console.WriteLine($"BeginDelete {DateTime.Now:O}");
var ar = targetShare.BeginDelete(result => {
Console.WriteLine($"Callback {DateTime.Now:O}");
}, null);
targetShare.EndDelete(ar);
}
try {
targetShare.Create();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
结果:
BeginDelete 2017-02-02T17:42:33.5303589+00:00
Callback 2017-02-02T17:42:33.6289211+00:00
The remote server returned an error: (409) Conflict.
每位官员 documentation,
When a share is deleted, a share with the same name cannot be recreated for at least 30 seconds. While the share is being deleted, attempts to recreate a share of the same name will fail with status code 409 (Conflict), with the service returning additional error information indicating that the share is being deleted. All other operations, including operations on any files under the share, will fail with status code 404 (Not Found) while the share is being deleted.
总之,只能在循环中不断重试Create(),并捕获409错误,直到成功创建共享。
我正在尝试删除并重新创建 Azure 存储文件共享,作为删除全部内容的快速方法。
问题是,当我尝试立即重新创建与之前删除的旧共享同名的新共享时,它失败了(出现 409 冲突)错误。如果我在删除它后等待大约 30 秒,它就可以正常工作。我认为这是因为它需要时间来释放共享名称。
这是我的代码:
var targetAccount = new CloudStorageAccount(new StorageCredentials(destination.StorageAccountName, destination.Key), true);
var targetClient = targetAccount.CreateCloudFileClient();
var targetShare = targetClient.GetShareReference(destination.ShareName);
if (targetShare.Exists()) {
var ar = targetShare.BeginDelete(null, null);
targetShare.EndDelete(ar);
}
Thread.Sleep(30000);
targetShare.Create();
根据 MSDN 上的文档,EndDelete
应该会阻塞直到删除完成,那为什么不呢?
如何避免必须等待一段固定的时间才能完成删除?
(我也试过这个的同步版本,但这完全一样)
更新
尝试了一些不同的东西,我想使用以下代码查看从 BeginDelete 回调的时间:
if (targetShare.Exists()) {
Console.WriteLine($"BeginDelete {DateTime.Now:O}");
var ar = targetShare.BeginDelete(result => {
Console.WriteLine($"Callback {DateTime.Now:O}");
}, null);
targetShare.EndDelete(ar);
}
try {
targetShare.Create();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
结果:
BeginDelete 2017-02-02T17:42:33.5303589+00:00
Callback 2017-02-02T17:42:33.6289211+00:00
The remote server returned an error: (409) Conflict.
每位官员 documentation,
When a share is deleted, a share with the same name cannot be recreated for at least 30 seconds. While the share is being deleted, attempts to recreate a share of the same name will fail with status code 409 (Conflict), with the service returning additional error information indicating that the share is being deleted. All other operations, including operations on any files under the share, will fail with status code 404 (Not Found) while the share is being deleted.
总之,只能在循环中不断重试Create(),并捕获409错误,直到成功创建共享。