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 5, 2018 in AWS by Kanishk
• 850 points
830 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 5, 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
• 3,770 points
1,501 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.