How do I use shell script to check if a bucket exists?

Posted on May 1, 2022

Question

I have aws cli installed. I'm just not sure how to do this in shell script.

when I run command aws s3 ls s3://bucket it would give me something like this

A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist

That means the bucket doesn't exist. So I want to run that from shell script and check if grep finds it. But my command doesn't work.

if [ $(aws s3 ls "s3://$S3_BUCKET" | grep 'NoSuchBucket' &> /dev/null) == 0 ] 
then
    echo "$S3_BUCKET doesn\'t exist please check again"
    exit
fi

It just gave me this

backup.sh: 20: [: 0: unexpected operator

Updated

I changed the script to be

echo "S3_BUCKET=$S3_BUCKET"
if aws s3 ls "s3://$S3_BUCKET" | grep -q 'AllAccessDisabled'    
then
    echo "$S3_BUCKET doesn\'t exist please check again"
    exit
fi

And this is the output I got

A client error (AllAccessDisabled) occurred when calling the ListObjects operation: All access to this object has been disabled

So the text contains AllAccessDisabled but I still don't the echo the next line.

Answer

The s3api head-bucket is more direct and does not incur the expense of listing a bucket with many files.

http://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html

if aws s3api head-bucket --bucket "$S3_BUCKET" 2>/dev/null; then