How to check wether a bucket exists using boto3

0 votes
I want to check whether a bucket exists or not using boto3. How to achieve that?
Dec 6, 2018 in AWS by Kanishk
• 890 points
16,180 views

1 answer to this question.

0 votes

You can use this code to check whether the bucket is available or not

import boto3
s3 = boto3.resource('s3')
print(s3.Bucket('priyajdm') in s3.buckets.all())
answered Dec 6, 2018 by Rishav

This could be very expensive call depending on how many times the all() must ask AWS for next bucket. 

Instead check creation_date: if it is None then it doesn't exist:

import boto3
s3 = boto3.resource('s3')
print(
    "Bucket does not exist" 
    if s3.Bucket('priyajdm').creation_date is None 
    else 
    "Bucket exists")
@schollii, that is correct, s3.bucket.all() will be an expensive query to run all the time. One can filter by creation_date, this will be easier.

Related Questions In AWS

0 votes
1 answer

How to download the latest file in a S3 bucket using AWS CLI?

You can use the below command $ aws ...READ MORE

answered Sep 6, 2018 in AWS by Archana
• 4,170 points
19,744 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP