Skip to content

Instantly share code, notes, and snippets.

@matriphe
Created May 5, 2024 10:08
Show Gist options
  • Save matriphe/2617fcc4e0031e343eb17b01c9858c50 to your computer and use it in GitHub Desktop.
Save matriphe/2617fcc4e0031e343eb17b01c9858c50 to your computer and use it in GitHub Desktop.
Restore AWS S3 Object from Glacier Type
#!/bin/sh
# Original: https://stackoverflow.com/a/55499152
BUCKET=bucket # Replace with your actual bucket name
BPATH=path # Replace with the path within the bucket
DAYS=5 # Number of days to keep in the Standard type
S3BUCKET="s3://$BUCKET$BPATH"
echo "Restoring files from bucket $S3BUCKET with retention period of $DAYS days."
echo "It will take some times."
# Loop through each object and restore it
for object_key in $(aws s3 ls $S3BUCKET --recursive | awk '{print $4}'); do
echo "Restoring: $object_key"
# Restore the object with error handling
if ! aws s3api restore-object --bucket $BUCKET --key $object_key --restore-request Days=$DAYS,GlacierJobParameters={"Tier"="Standard"} &> /dev/null; then
echo " ** Error restoring $object_key. Check AWS CLI output for details."
fi
echo "Monitoring: $object_key"
# Monitor the object with error handling
if ! aws s3api head-object --bucket $BUCKET --key $object_key &> /dev/null; then
echo " ** Error monitoring $object_key. Check AWS CLI output for details."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment