How to print the names of all the bucket in S3 in Python

0 votes
How to print the names of all the bucket in S3 in Python using boto3?
Nov 30, 2018 in AWS by Nitesh
• 3,080 points
14,047 views

1 answer to this question.

0 votes

You can use a for loop to loop around the buckets in your S3. In boto3 there is a fucntion that helps this task go easier.(buckets.all())

You can use the following program to print the names of bucket

import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print (bucket.name)

Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Training today.

Thanks!

answered Nov 30, 2018 by Aniket
Thanks.  That Helped
Hi,

If i want to return this listed bucket.information from a function, how could this be done ?

Yes, you can do that I guess. Create one function as given below. It should work.

def fun():
    import boto3
    s3 = boto3.resource('s3')
    for bucket in s3.buckets.all():
        print (bucket.name)
    return bucket.name

Now call the function.

fun()
Suppose i have 10 buckets in my AWS account and i want to list only 5 buckets. How could this be done ?
Hi,

You can create one if-else block and use one counter. If it reaches 5 break your loop.
I mean, not just random 5 buckets out of 10.  I want to list specific 5 buckets out of 10.
Example: There are buckets named: a, b, c, d, e, f, g, h, i,  j...... I want to list a, d, f, h, j out of them.

How could this be achieved ?

You have to create your own customize code according to your use case. I don't think any inbuilt function is available.

Related Questions In AWS

0 votes
1 answer

How to measure the total size of an S3 bucket using python?

Hi@akhtar, You can do this tasks using Boto. ...READ MORE

answered Apr 15, 2020 in AWS by MD
• 95,360 points
3,315 views
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,150 points
15,070 views
0 votes
1 answer

How to list all the objects in Amazon S3?

As stated already, Amazon S3 indeed requires ...READ MORE

answered Oct 5, 2018 in AWS by Archana
• 4,150 points
5,514 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,080 points
3,400 views
–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,080 points
3,626 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,810 points
1,944 views
0 votes
2 answers
0 votes
1 answer

How to create a S3 bucket in AWS Console?

To create a S3 bucket in AWS, ...READ MORE

answered Feb 13, 2019 in AWS by Priyaj
• 58,080 points
561 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP