How to calculate bucket storage size using AWS CLI
Find the exact storage used by one or all buckets, including previous object versions
Overview
The IC Storage Console shows object counts per bucket but not total storage size. Use AWS CLI to calculate the exact size of one or all buckets from the command line.
If versioning is enabled, previous object versions still consume storage and count toward billing. The commands below cover both current-only and version-aware calculations.
Measuring the size of a bucket using AWS CLI requires listing of all objects in the bucket. This may take from seconds to hours for buckets with dozens of millions of objects.
Prerequisites
- Install AWS CLI
- Configure your IC credentials with your Access Key ID and Secret Access Key
All examples use region eu-central-2. Replace the region in both --endpoint-url and --region if your bucket is in a different region.
One bucket (current objects only)
aws s3 ls s3://my-bucket --recursive --summarize --endpoint-url https://eu-central-2.storage.impossibleapi.net --region eu-central-2
The last two lines of output show total object count and total size.
One bucket (including all versions)
If versioning is enabled, old versions are not included in the command above. To get the true billable size:
aws s3api list-object-versions --bucket my-bucket --query "sum(Versions[].Size)" --output text --endpoint-url https://eu-central-2.storage.impossibleapi.net --region eu-central-2
Each line returns total bytes for each page of listed objects (up to 1000 objects per page). Sum all lines to get the total value.
Note: For buckets with millions of object versions, this command may take several hours to paginate through all results.
All buckets (including versions, with error handling)
These scripts loop through every bucket, calculate total size including all object versions, warn if a bucket is not accessible, and show a grand total.
Bash
#!/bin/bash
EP="https://eu-central-2.storage.impossibleapi.net"
RG="eu-central-2"
grand=0
for b in $(aws s3api list-buckets --query "Buckets[].Name" --output text --endpoint-url "$EP" --region "$RG"); do
raw=$(aws s3api list-object-versions --bucket "$b" --query "Versions[].Size" --output text --endpoint-url "$EP" --region "$RG" 2>/dev/null)
if [ $? -ne 0 ]; then echo "WARNING: cannot read $b (access denied)"; continue; fi
if [ -z "$raw" ] || [ "$raw" = "None" ]; then
size=0
else
size=$(echo "$raw" | tr '\t' '\n' | awk '{s+=$1} END {printf "%.0f", s}')
fi
grand=$((grand + size))
echo "$b: $(numfmt --to=iec "$size" 2>/dev/null || echo "${size} bytes")"
done
echo "---"
echo "Total: $(numfmt --to=iec "$grand" 2>/dev/null || echo "${grand} bytes")"
Note:
numfmtis part of GNU coreutils, preinstalled on most Linux systems and Git Bash for Windows. On macOS, install viabrew install coreutils.
PowerShell
$EP = "https://eu-central-2.storage.impossibleapi.net"
$RG = "eu-central-2"
$grand = [long]0
$buckets = (aws s3api list-buckets --query "Buckets[].Name" --output text --endpoint-url $EP --region $RG) -split "`t"
foreach ($b in $buckets) {
$raw = aws s3api list-object-versions --bucket $b `
--query "Versions[].Size" --output text `
--endpoint-url $EP --region $RG 2>$null
if ($LASTEXITCODE -ne 0) { Write-Warning "Cannot read $b (access denied)"; continue }
$size = [long]0
if ($raw -and $raw -ne "None") {
$size = ("$raw" -split '\s+') |
ForEach-Object { [long]$_ } |
Measure-Object -Sum |
Select-Object -ExpandProperty Sum
}
$grand += $size
Write-Host ("{0}: {1:N2} GiB" -f $b, ($size / 1GB))
}
Write-Host "---"
Write-Host ("Total: {0:N2} GiB" -f ($grand / 1GB))