Bootstrap FreeKB - Amazon Web Services (AWS) - List S3 Buckets using Python boto3
Amazon Web Services (AWS) - List S3 Buckets using Python boto3


This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.

Here is the minimal boilerplate code without any error handling to list your S3 Buckets

#!/usr/bin/python3
import boto3

client = boto3.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")

 

Here is a more practical example, with try/except/else error handling.

#!/usr/bin/python3
import boto3
import sys

try:
  client = boto3.client('s3')
except Exception as exception:
  print(exception)
  sys.exit(1)

try:
  s3_buckets = client.list_buckets()
except Exception as exception:
  print(exception)
else:
  print(s3_buckets)

 

Which should return something  that contains something like this.

'Buckets': [
    {'Name': 'my-bucket-foo', 
     'CreationDate': datetime.datetime(2023, 7, 21, 23, 17, 2, tzinfo=tzutc())
    }, 
    {'Name': 'my-bucket-bar', 
     'CreationDate': datetime.datetime(2023, 8, 21, 11, 9, 20, tzinfo=tzutc())
    }
  ], 
}

 

The "default" profile in your .aws/credentials file will be used. Session can be used to use some other profile.

#!/usr/bin/python3
import boto3

session = boto3.Session(profile_name='johndoe')
client = session.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")

 

Or, os.environ['AWS_PROFILE'] can be used to specify the profile in /home/john.doe/.aws/config and /home/john.doe/.aws/credentials to use.

import boto3
import os

os.environ['AWS_PROFILE'] = 'johndoe'

client = boto3.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")

 

And here is how you can return a bucket using the bucket Tags, such as "environment".

client = boto3.client('s3')
s3_buckets = client.list_buckets()

buckets = s3_buckets['Buckets']

for bucket in buckets:
    tags = client.get_bucket_tagging(Bucket=bucket['Name'])

    for item in tags['TagSet']:
        if item['Key'] == 'environment' and item['Value'] == 'staging':
            s3bucket = bucket['Name']
            break

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 79d125 in the box below so that we can be sure you are a human.