Advertisement
matthewdeanmartin

delete_old_files_from_s3.sh

Dec 4th, 2022 (edited)
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.92 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Set the bucket name
  4. BUCKET_NAME="my-s3-bucket"
  5.  
  6. # Set the threshold for file age (in seconds)
  7. THRESHOLD=$((3 * 30 * 24 * 60 * 60))
  8.  
  9. # Get the current time
  10. NOW=$(date +%s)
  11.  
  12. # List all the objects in the bucket
  13. OBJECTS=$(aws s3api list-objects --bucket $BUCKET_NAME | jq -r '.Contents[].Key')
  14.  
  15. # Loop through the objects and delete any that are older than the threshold
  16. for OBJECT in $OBJECTS
  17. do
  18.   # Get the last modified time of the object
  19.   LAST_MODIFIED=$(aws s3api head-object --bucket $BUCKET_NAME --key $OBJECT | jq -r '.LastModified')
  20.  
  21.   # Convert the last modified time to a timestamp
  22.   LAST_MODIFIED_TIMESTAMP=$(date -d "$LAST_MODIFIED" +%s)
  23.  
  24.   # Calculate the age of the object
  25.   AGE=$((NOW - LAST_MODIFIED_TIMESTAMP))
  26.  
  27.   # Delete the object if it is older than the threshold
  28.   if [ $AGE -gt $THRESHOLD ]
  29.   then
  30.     aws s3api delete-object --bucket $BUCKET_NAME --key $OBJECT
  31.   fi
  32. done
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement