l3_08
l3_08

Reputation: 191

Get the names and count of all buckets in AWS S3

I'm new to Boto3 and AWS API, and I want to get the list of buckets' names and the count of the available buckets in S3.

Any help is appreciated.

Upvotes: 4

Views: 4727

Answers (2)

helloV
helloV

Reputation: 52375

To get all buckets in your account:

import boto3

s3 = boto3.resource('s3')
bucket_list = [bucket.name for bucket in s3.buckets.all()]
print len(bucket_list)
print bucket_list

Upvotes: 6

Roshan
Roshan

Reputation: 1390

This script will help you to list all the bucket names and also get the count.

import boto 
from boto.s3.connection import OrdinaryCallingFormat

conn = boto.connect_s3(calling_format=OrdinaryCallingFormat())

count = 0

print ("Bucket names: ")

for bucket in conn.get_all_buckets():
    print (bucket.name)
    count = count + 1

print ("Total count of S3 bucket is ", count)

Note : please specify aws keys in script if you are not yet specified it in .boto file

Hope it helps !!

Upvotes: 0

Related Questions