Following on from the https://www.geekmungus.co.uk/aws-cli-quick-start-linux guide, let’s see about how you can connect to your AWS from Python3 using Boto3.
If you’ve not already done it, and if you’re not using the “default” credentials, you can specify a particular profile to use by adding it to an environment variable for example the below; where the “londonaws” is the profile name as written in the ~/.aws/config file.
export AWS_PROFILE=londonaws
In my example I’m assuming Python3 is already installed, so we’ll create a virtual environment first with:
sudo apt install python3-virtualenv
mkdir venv
cd venv
virtualenv aws1
source /home/<username>/venv/aws1/bin/activate
As we’ve switched into our virtualenv with that last command (source) we can then install boto3 using pip.
pip install boto3
Right now we’re ready to rumble, assuming you have an active session open with AWS, the script using boto3 can make use of the existing AWS session. If you get any access errors see the previous guide (link at the top of this post) to ensure you have an active session.
Now let’s create a cheeky script to just access AWS and pull out a list of buckets, create the following python script file, ensure you mark it as executable when finished with: chmod u+x test.py.
#!/usr/bin/env python
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
If we then run this script we should see an output that displays the names of any buckets within our S3 storage.
Again the boto3 documentation is pretty good to help you get started see the link:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html