【问题标题】:How to remove files before a particular time period from azure file share如何从 azure 文件共享中删除特定时间段之前的文件
【发布时间】:2021-12-24 01:09:28
【问题描述】:

如何从 Azure 存储帐户的**文件共享**中删除旧文件。任何 AZ CLI 命令或从 Azure 中的文件共享中清除旧文件的代码?

【问题讨论】:

  • 如果您认为答案有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充)。这对其他社区成员可能是有益的。谢谢。

标签: azure azure-storage-account fileshare


【解决方案1】:

请检查此电源外壳代码:

注意:为了安全起见,请在任何删除活动之前查看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

参考:

  1. deleting-files-in-azure-file-share-older-than-x-days | Microsoft Q & A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2021-12-23
    • 2021-12-06
    • 2021-09-30
    • 2022-10-19
    • 2021-10-20
    • 2021-05-08
    相关资源
    最近更新 更多