【发布时间】:2021-12-24 01:09:28
【问题描述】:
如何从 Azure 存储帐户的**文件共享**中删除旧文件。任何 AZ CLI 命令或从 Azure 中的文件共享中清除旧文件的代码?
【问题讨论】:
-
如果您认为答案有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充)。这对其他社区成员可能是有益的。谢谢。
标签: azure azure-storage-account fileshare
如何从 Azure 存储帐户的**文件共享**中删除旧文件。任何 AZ CLI 命令或从 Azure 中的文件共享中清除旧文件的代码?
【问题讨论】:
标签: azure azure-storage-account fileshare
请检查此电源外壳代码:
注意:为了安全起见,请在任何删除活动之前查看Best practices。
通过在文件共享中递归列出文件/FileDir 来删除超过 14 天的文件共享的示例:
$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
$shareName = <shareName>
$DirIndex = 0
$dirsToList = New-Object System.Collections.Generic.List[System.Object]
# Get share root Dir
$shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx
$dirsToList += $shareroot
# List files recursively and remove file older than 14 days
While ($dirsToList.Count -gt $DirIndex)
{
$dir = $dirsToList[$DirIndex]
$DirIndex ++
$fileListItems = $dir | Get-AzStorageFile
$dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
$dirsToList += $dirsListOut
$files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
foreach($file in $files)
{
# Fetch Attributes of each file and output
$task = $file.CloudFile.FetchAttributesAsync()
$task.Wait()
# remove file if it's older than 14 days.
if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-14))
{
## print the file LMT
# $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } }
# remove file
$file | Remove-AzStorageFile
}
}
#Debug log
# Write-Host $DirIndex $dirsToList.Length $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString()
}
你也可以试试Azure data factory
参考:
【讨论】: