Bootstrap FreeKB - Amazon Web Services (AWS) - Getting Started with Python boto3
Amazon Web Services (AWS) - Getting Started with Python boto3


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? Lets 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
[profile johndoe]
region = us-east-1

 

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

 

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 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 Buy Me A Coffee



Comments


Add a Comment


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