简体   繁体   English

我如何知道何时删除了 Azure 文件存储共享?

[英]How do I know when an Azure File Storage share has been deleted?

I'm trying to delete and recreate an Azure Storage file share as a quick way to delete the entire contents.我正在尝试删除并重新创建 Azure 存储文件共享,以快速删除整个内容。

The problem is that when I try to immediately re-create the new share with the same name as the old previously deleted share it fails (with 409 Conflict) error.问题是,当我尝试立即重新创建与以前删除的旧共享同名的新共享时,它会失败(出现 409 冲突)错误。 If I wait around 30 seconds after deleting it, it works fine.如果我在删除它后等待大约 30 秒,它工作正常。 I assume this is because it needs time to free up the share name.我认为这是因为它需要时间来释放共享名称。

Here's my code:这是我的代码:

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();

According to the documentation on MSDN, EndDelete is suppose to block until the delete has completed, so why isn't it?根据 MSDN 上的文档, EndDelete假设阻塞直到删除完成,那为什么不是呢?

How can I avoid having to wait a fixed amount of time for the delete to complete?如何避免必须等待固定的时间才能完成删除?

(I've also tried the synchronous version of this as well, but this does exactly the same) (我也试过这个同步版本,但这完全一样)

UPDATE更新

Trying a few different things, I wanted to see the timings on the call back from BeginDelete using this code:尝试了一些不同的事情,我想使用以下代码查看从 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);
}

Result:结果:

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.

Per official documentation ,根据官方文档

When a share is deleted, a share with the same name cannot be recreated for at least 30 seconds.删除共享后,至少 30 秒内无法重新创建同名共享。 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.在删除共享时,尝试重新创建同名共享将失败,状态代码为 409(冲突),服务会返回其他错误信息,指示正在删除共享。 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.在删除共享时,所有其他操作,包括对共享下任何文件的操作,都将失败并显示状态代码 404(未找到)。

In conclusion, you can only keep retrying Create() in the loop and catch 409 error, until the share is successfully created.总之,您只能在循环中不断重试 Create() 并捕获 409 错误,直到成功创建共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM