forked from swoodford/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-buckets-total-file-size.sh
executable file
·167 lines (144 loc) · 4.4 KB
/
s3-buckets-total-file-size.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
# Script to count total size of all data stored in a single or in all S3 buckets
# Requires aws s3api, jq, IAM account must have permission to access all buckets
# Functions
# Fail
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
# Check for command
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Completed
function completed(){
echo
HorizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
HorizontalRule
echo
}
# Horizontal Rule
function HorizontalRule(){
echo "============================================================"
}
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi
# Check for AWS CLI profile argument passed into the script
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-multiple-profiles
if [ $# -eq 0 ]; then
scriptname=`basename "$0"`
echo "Usage: ./$scriptname profile"
echo "Where profile is the AWS CLI profile name"
echo "Using default profile"
echo
profile=default
else
profile=$1
fi
# Check required commands
check_command "aws"
check_command "jq"
# Convert bytes to human readable
function bytestohr(){
SLIST="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"
POWER=1
VAL=$( echo "scale=2; $1 / 1" | bc)
VINT=$( echo $VAL / 1024 | bc )
while [ $VINT -gt 0 ]
do
let POWER=POWER+1
VAL=$( echo "scale=2; $VAL / 1024" | bc)
VINT=$( echo $VAL / 1024 | bc )
done
echo $VAL $( echo $SLIST | cut -f$POWER -d, )
}
# One bucket or all buckets
function choiceMenu(){
tput smul; echo "Single S3 bucket or all buckets?" && tput sgr0
echo 1. Single bucket
echo 2. All buckets
echo
read -r -p "Menu selection #: " menuSelection
case $menuSelection in
1)
SingleBucket
;;
2)
AllBuckets
;;
*)
fail "Invalid selection!"
;;
esac
}
function SingleBucket(){
read -r -p "Bucket name: s3://" CURRENTBUCKET
echo
echo "Calculating size..."
CURRENTBUCKETREGION=$(aws s3api get-bucket-location --bucket $CURRENTBUCKET --output text --profile $profile 2>&1)
if echo $CURRENTBUCKETREGION | grep -q None; then
REGION="us-east-1"
else
REGION=$CURRENTBUCKETREGION
fi
CURRENTBUCKETSIZE=$(aws s3api list-objects --bucket $CURRENTBUCKET --region $REGION --output json --query "[sum(Contents[].Size)]" --profile $profile 2>&1)
if echo $CURRENTBUCKETSIZE | grep -q invalid; then
CURRENTBUCKETSIZE="0"
else
CURRENTBUCKETSIZE=$(echo "$CURRENTBUCKETSIZE" | jq '.[]')
fi
echo
echo "Size: "
bytestohr $CURRENTBUCKETSIZE
completed
}
function AllBuckets(){
# List buckets
LS=$(aws s3 ls --profile $profile 2>&1)
# Count number of buckets
TOTALNUMBERS3BUCKETS=$(echo "$LS" | wc -l | rev | cut -d " " -f1 | rev)
# Get list of all bucket names
BUCKETNAMES=$(echo "$LS" | cut -d ' ' -f3 | nl)
echo
HorizontalRule
echo "Counting Total Size of Data in $TOTALNUMBERS3BUCKETS S3 Buckets"
echo "(This may take a very long time depending on number of files)"
HorizontalRule
echo
START=1
TOTALBUCKETSIZE=0
for (( COUNT=$START; COUNT<=$TOTALNUMBERS3BUCKETS; COUNT++ ))
do
CURRENTBUCKET=$(echo "$BUCKETNAMES" | grep -w [^0-9][[:space:]]$COUNT | cut -f2)
HorizontalRule
echo \#$COUNT $CURRENTBUCKET
CURRENTBUCKETREGION=$(aws s3api get-bucket-location --bucket $CURRENTBUCKET --output text --profile $profile 2>&1)
if echo $CURRENTBUCKETREGION | grep -q None; then
REGION="us-east-1"
else
REGION=$CURRENTBUCKETREGION
fi
CURRENTBUCKETSIZE=$(aws s3api list-objects --bucket $CURRENTBUCKET --region $REGION --output json --query "[sum(Contents[].Size)]" --profile $profile 2>&1)
if echo $CURRENTBUCKETSIZE | grep -q invalid; then
CURRENTBUCKETSIZE="0"
else
CURRENTBUCKETSIZE=$(echo "$CURRENTBUCKETSIZE" | jq '.[]')
fi
TOTALBUCKETSIZE=$(($TOTALBUCKETSIZE + $CURRENTBUCKETSIZE))
echo "Size: "
bytestohr $CURRENTBUCKETSIZE
echo "Subtotal: "
bytestohr $TOTALBUCKETSIZE
done
completed
echo "Total Size of Data in All $TOTALNUMBERS3BUCKETS S3 Buckets:"
bytestohr $TOTALBUCKETSIZE
}
choiceMenu