AWS¶
boto3 library¶
A unique library to access all AWS services from a python app.
Installation¶
pip install boto3[crt]
Set up authentication credentials for your AWS account using either the IAM Console or the AWS CLI.
aws configure
# Verify access
aws iam list-users
Info
The jbcodeforce/python docker image has the aws cli and goto3.
Programming samples¶
Access S3¶
import boto3
# declare a client to the service you want
s3 = goto3.service("s3")
# use SDK API for s3.
s3.buckets.all()
Access DynamoDB¶
The client can get the table name using the API client:
import os, boto3
AWS_ACCESS_KEY_ID=os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY=os.environ.get("AWS_SECRET_ACCESS_KEY")
client = boto3.client(
'dynamodb',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
table = client.list_tables()
tableName=table['TableNames'][0]
Then use the dynamoDB API:
orderTable = dynamodb.Table(tableName)
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
orderTable = dynamodb.Table(tableName)
orderTable.put_item(
Item={
"orderID": "ORD001",
"customerID": "C01",
"productID": "P01",
"quantity": 10,
"destinationAddress": { "street": "1st main street", "city": "Santa Clara", "country": "USA", "state": "CA", "zipcode": "95051" }
})
- Run it once the python virtual env is enabled with
python dynamoClient.py
- scan to run all items from a table. It performs eventually consistent reads
- put_item: If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item
CDK with python¶
Separate note in AWS_Studies.