How to delete or purge old files on S3?

前端 未结 5 1793
盖世英雄少女心
盖世英雄少女心 2020-12-24 10:21

Are there existing solutions to delete any files older than x days?

5条回答
  •  猫巷女王i
    2020-12-24 10:59

    You can use the following Powershell script to delete object expired after x days.

    [CmdletBinding()]
    Param(  
      [Parameter(Mandatory=$True)]
      [string]$BUCKET_NAME,             #Name of the Bucket
    
      [Parameter(Mandatory=$True)]
      [string]$OBJ_PATH,                #Key prefix of s3 object (directory path)
    
      [Parameter(Mandatory=$True)]
      [string]$EXPIRY_DAYS             #Number of days to expire
    )
    
    $CURRENT_DATE = Get-Date
    $OBJECTS = Get-S3Object $BUCKET_NAME -KeyPrefix $OBJ_PATH
    Foreach($OBJ in $OBJECTS){
        IF($OBJ.key -ne $OBJ_PATH){
            IF(($CURRENT_DATE - $OBJ.LastModified).Days -le $EXPIRY_DAYS){
                Write-Host "Deleting Object= " $OBJ.key
                Remove-S3Object -BucketName $BUCKET_NAME -Key $OBJ.Key -Force
            }
        }
    }
    

提交回复
热议问题