Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3: Using boto3.resource('s3') to list all S3 buckets

Is it possible to list all S3 buckets using a boto3 resource, ie boto3.resource('s3')?

I know that it's possible to do so using a low-level service client:

import boto3
boto3.client('s3').list_buckets()

However in an ideal world we can operate at the higher level of resources. Is there a method that allows us to to do and, if not, why?

like image 225
Eytan Avatar asked Mar 19 '18 21:03

Eytan


2 Answers

You can use s3.buckets.all():

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

Using list comprehension:

s3 = boto3.resource('s3')
buckets = [bucket.name for bucket in s3.buckets.all()]
print(buckets)
like image 71
helloV Avatar answered Oct 25 '22 04:10

helloV


Get .buckets.pages() from the S3 resource and then loop through the pages to grab the buckets:

import boto3


buckets_iter = boto3.resource('s3').buckets.pages()
buckets = []
for bucket in buckets_iter:
    buckets += bucket

print(buckets)

I hope this helps.

like image 39
Abdou Avatar answered Oct 25 '22 05:10

Abdou