Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Set the bucket name
- BUCKET_NAME="my-s3-bucket"
- # Set the threshold for file age (in seconds)
- THRESHOLD=$((3 * 30 * 24 * 60 * 60))
- # Get the current time
- NOW=$(date +%s)
- # List all the objects in the bucket
- OBJECTS=$(aws s3api list-objects --bucket $BUCKET_NAME | jq -r '.Contents[].Key')
- # Loop through the objects and delete any that are older than the threshold
- for OBJECT in $OBJECTS
- do
- # Get the last modified time of the object
- LAST_MODIFIED=$(aws s3api head-object --bucket $BUCKET_NAME --key $OBJECT | jq -r '.LastModified')
- # Convert the last modified time to a timestamp
- LAST_MODIFIED_TIMESTAMP=$(date -d "$LAST_MODIFIED" +%s)
- # Calculate the age of the object
- AGE=$((NOW - LAST_MODIFIED_TIMESTAMP))
- # Delete the object if it is older than the threshold
- if [ $AGE -gt $THRESHOLD ]
- then
- aws s3api delete-object --bucket $BUCKET_NAME --key $OBJECT
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement