#!/bin/bash # This script counts the number of objects stored in the Glacier storage class within an S3 bucket. # It optionally filters the list based on a specified directory within the bucket. # Example usage: # ./count-glacier-objects.sh my-bucket my-directory # (Counts Glacier objects in the 'my-directory' directory within the 'my-bucket' bucket) # ./count-glacier-objects.sh my-bucket # (Counts all Glacier objects in the 'my-bucket' bucket) # Check the number of arguments if [[ $# -lt 1 || $# -gt 2 ]]; then echo "Usage: $0 [directory_name]" exit 1 fi # Retrieve the arguments BUCKET="$1" DIRECTORY="$2" # Function to sanitize names (though not necessary for an output file in this version) sanitize_name() { echo "$1" | tr -dc '[:alnum:]-_.' } # Construct the AWS CLI command COMMAND="aws s3api list-objects-v2 --bucket $BUCKET --query \"Contents[?StorageClass=='GLACIER']\"" # Add the prefix if a directory is specified if [[ -n "$DIRECTORY" ]]; then COMMAND+=" --prefix $DIRECTORY" fi # Finalize the command to count the objects COMMAND+=" --output text | wc -l" # Execute the command and retrieve the number of Glacier files NUM_FILES=$(eval $COMMAND) # Display the number of Glacier files echo "Number of files in Glacier storage class to be restored: $NUM_FILES"