gpt4 book ai didi

c# - 如何知道 Azure 文件存储共享何时已被删除?

转载 作者:行者123 更新时间:2023-12-03 03:08:32 25 4
gpt4 key购买 nike

我正在尝试删除并重新创建 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错误,直到共享创建成功。

关于c# - 如何知道 Azure 文件存储共享何时已被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com