Wednesday, February 9, 2022

Get a list of empty s3 buckets | aws | sh

Note: Before Running this script please configure your AWS CLI.

#!/bin/bash
for bucketlist in  $(aws s3api list-buckets | jq --raw-output '.Buckets[].Name');
do
  echo "* $bucketlist"
  listobjects=$(\
      aws s3api list-objects --bucket $bucketlist \
      --query 'Contents[*].Key')
  #echo "==$listobjects=="
    if [[ "$listobjects" == "null" ]]; then
          echo "$bucketlist is empty"
  echo $bucketlist >> emptyBucketList.txt
    fi
done

No comments:

Post a Comment