
Python 3 uses the boto3 package to interact with Amazon Web Services (AWS). The pip list command to determine if the boto3 package is installed on your system.
~]$ pip list
Package Version
------------------- ---------
boto3 1.23.10
If the boto3 package is not listed, the pip install command can be used to install the boto3 package.
pip install boto3
Here is the minimal boilerplate code without any error handling to list your S3 Buckets using Python boto3.
#!/usr/bin/python3
import boto3
client = boto3.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")
So, how does authentication work? Let's say you have set Amazon Web Services (AWS) Profile Config on the same system as your Python program.
- Linux / Mac = /home/john.doe/.aws/config
- Windows = C:\Users\your_username\.aws\config (if this file does not exist, it can be created using the PowerShell New-Item and Add-Content cmdlets)
For example, let's say the "config" file contains something like this.
[default]
region = us-east-1
profile = johndoe
output = json
[profile johndoe]
region = us-east-1
output = json
[profile janedoe]
region = us-east-1
output = json
Likewise, the "credentials" file contains something like this.
- Linux / Mac = /home/john.doe/.aws/credentials
- Windows = C:\Users\your_username\.aws\credentials
[default]
aws_secret_access_key = abcdefg123456789abcdefg123456789abcdefg1
aws_access_key_id = ABCDEFG123456789ABCD
[johndoe]
aws_secret_access_key = zxcasdqwe987654321zxsdqwe3213654987zxcsd
aws_access_key_id = DFMVKFJSM456789SDFSB
[janedoe]
aws_secret_access_key = pftasdqwe987654321zxsdqwe3213654987zxzya
aws_access_key_id = AKB123ABLC2349dAD93Z
In this scenario, when connecting to AWS, the "default" profile would be used.
#!/usr/bin/python3
import boto3
client = boto3.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")
Or you can use os to specify a specific config and credentials file.
#!/usr/bin/python3
import boto3
os.environ['AWS_CONFIG_FILE'] = '/path/to/.aws/config'
os.environ['AWS_SHARED_CREDENTIALS_FILE'] = '/path/to/.aws/credentials'
client = boto3.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")
Or you can create a session using a named 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, you can create a session using an Access Key, Secret Key, and Region.
#!/usr/bin/python3
import boto3
session = boto3.Session(
aws_access_key_id="ABDE1234ADSFB12345SD",
aws_secret_access_key="abcdefg123456789abdefg123456789abdefg123",
region_name="us-east-1"
)
client = session.client('s3')
s3_buckets = client.list_buckets()
print(f"s3_buckets = {s3_buckets}")
Did you find this article helpful?
If so, consider buying me a coffee over at