azure - 无法删除 Azure 中的目录

标签 azure file azure-storage azure-files azure-storage-files

我在 Azure 中有一个文件共享,其中包含文件夹,而文件夹内又包含许多文件夹。 我试图通过右键单击该文件夹来手动删除该文件夹,该文件夹内有很多文件,并且显示

Failed to delete directory. Error: The specified directory is not empty.

如何删除该目录?需要删除的目录中有数千个文件,无法手动删除每个文件来删除目录

最佳答案

更新:

您可以使用Azure Storage Explorer(请参阅 this article 了解如何安装和使用它。),然后导航到您的文件共享 -> 右键单击​​文件夹 -> 选择删除。这可以删除非空文件夹。

或者您可以将 AzCopy(有关此工具的更多详细信息请参阅 here)与 azcopy remove 结合使用。命令和 --recursive 参数。

<小时/>

原文:

无法删除 azure 文件共享中的非空文件夹,您应该首先删除其中的所有文件。

请考虑为此目的编写一些代码。还有一个article它使用 powershell 删除非空文件夹。这是本文使用的powershell代码(您也可以在github here找到源代码):

function RemoveFileDir ([Microsoft.Azure.Storage.File.CloudFileDirectory] $dir, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext] $ctx)
{   
    $filelist = Get-AzStorageFile -Directory $dir
    
    foreach ($f in $filelist)
    {   
        if ($f.GetType().Name -eq "CloudFileDirectory")
        {
            RemoveFileDir $f $ctx #Calling the same unction again. This is recursion.
        }
        else
        {
            Remove-AzStorageFile -File $f           
        }
    }
    Remove-AzStorageDirectory -Directory $dir
    
} 


#define varibales
$StorageAccountName = "Your Storage account name" 
$StorageAccountKey = "Your storage account primary key"
$AzShare = "your azure file share name"
$AzDirectory = "LatestPublish - your directory name under which you want to delete everything; including this directry"
 
 

#create primary region storage context
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$ctx.ToString()

#Check for Share Existence
$S = Get-AzStorageShare -Context $ctx -ErrorAction SilentlyContinue|Where-Object {$_.Name -eq $AzShare}

# Check for directory
$d = Get-AzStorageFile -Share $S -ErrorAction SilentlyContinue|select Name

if ($d.Name -notcontains $AzDirectory)
{
    # directory is not present; no action to be performed
    
}
else
{    
    $dir = Get-AzStorageFile -Share $s -Path $AzDirectory    
    RemoveFileDir $dir $ctx    
}

关于azure - 无法删除 Azure 中的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63887143/

相关文章:

unit-testing - 在单元测试中模拟 azure blob 存储

c# - WebJob ServiceBus 主题权限

c - 数据不会被写入 C 中的文件中

azure - 机器人 channel 注册在迁移后自动分配位置 "global"

python - 如何在 .cfg 文件中存储带 # 的单词

c - 如何在 C 中将基于文本的数据文件读入数组?

azure - Azure CDN 和同一数据中心内的托管应用程序之间是否收费?

python - Azure 函数 (python) 将输出写入输出 blob 路径的动态命名

javascript - 使用 Node 缩略图从图像生成缩略图

powershell - 获取控制台模式时发生 Win32 内部错误 "The handle is invalid"0x6