Boto3 is an AWS SDK for Python. It provides an object oriented API services and low level services to the AWS services. It allows users to create, and manage AWS services such as EC2 and S3.
In this tutorial, you’ll learn
- What is the difference between AWS Boto3 session, client and resource
- What is AWS session, client and resources
- How to create them using the AWS Credentials directly or using Environment variables
Boto3 Session vs Boto3 Client vs Boto3 Resource
|Create service clients and resources||Low level AWS service Class||High level, object-oriented API|
|Session is an object to create a connection to AWS Service and manage the state of the connection||It provides methods to connect with AWS services similar to the AWS API service.||It represents the Object oriented interface to AWS services.|
|All API services are available in Boto3 Client. Maps 1:1 with the AWS service API||Not all API services are available in the resource. It has actions() defined to make calls to the AWS services.|
|Uses Snake case notation for the naming conventions. e.g.: list_buckets()||Uses UpperCaseCamelCase notation for the naming convention. Because resource is similar to classes. Python classes follow this notation.|
When you want to use the environment variables for specifying the AWS credentials, you should have already configured the AWS Cli in your machine. The configuration will be stored in the location
To know more about how to install and Configure AWS client, read How to Install AWS Cli on Ubuntu and Configure AWS Cli?
Boto3 session is an object to create a connection to your AWS service and manage the connection state throughout your program life cycle.
Create Boto3 Session
You can create
Boto3 session using your AWS credentials Access key id and Secret access key.
Creating a Boto3 Session by Directly Specifying the Credentials
In the section, you’ll directly specify the AWS credentials for creating boto3 session.
Boto3 Session Example
#Creating Session With Boto3. session = boto3.Session( aws_access_key_id='Your Access Key ID', aws_secret_access_key='You Secret access key' )
Creating a Boto3 Session by Using ENVIRONMENT Variables
In this section, you’ll create Boto3 session by using the environmental variables for specifying the AWS Credentials.
Boto3 Session Example
#Creating Session With Boto3. session = boto3.Session( aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY )
Boto3 client is a low level service class to connect to AWS service. It provides the similar methods available in the AWS API. All the methods available in the AWS API are available in the Boto3 client.
Create Boto3 Client
You can create AWS Boto3 client using your AWS credentials Access key id and Secret access key.
Creating a Boto3 Client by Directly Specifying the Credentials
In the section, you’ll directly specify the AWS credentials for creating boto3 client.
Boto3 Client Example
s3_client = boto3.client('s3', aws_access_key_id='Your Access Key ID', aws_secret_access_key='You Secret access key' region_name='YOUR_REGION_NAME' )
Creating a Boto3 Client by Using ENVIRONMENT Variables
In this section, you’ll create Boto3 client by using the environmental variables for specifying the AWS Credentials.
You can use the below code to create a Boto3 client.
Boto3 Client Example
s3_client = boto3.client('s3', aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY, region_name=REGION_NAME )
Boto3 resource is a high level object oriented API service you can use to connect and access your AWS resource. It has
actions() defined which can be used to make call to the AWS service.
Create a Boto3 Resource
You can create a Boto3 resource using the session you’ve already created. In this section, assume that, you have created a session to Boto3 in the variable called
Now, you’ll create a
Boto3 resource with the
Resource() accepts the AWS service name as a parameter. In the below example, you’ll create an S3 resource which can be used to perform operations in your S3 objects.
Boto3 Resource Example
s3 = session.resource('s3')
In this tutorial, you’ve learnt the difference between Boto3 resource, client and session.
You have also learnt how to create Boto3 resource, client and session directly by specifying the AWS credentials or using the AWS configuration environment variables.