0
# Session Management
1
2
Core session functionality for creating and configuring AWS service clients and resources with async context management. The Session class serves as the main entry point for all aioboto3 operations.
3
4
## Capabilities
5
6
### Session Creation
7
8
Creates a new aioboto3 session with AWS credentials and configuration. The session manages authentication, region settings, and provides methods to create clients and resources.
9
10
```python { .api }
11
class Session:
12
def __init__(
13
self,
14
aws_access_key_id: str = None,
15
aws_secret_access_key: str = None,
16
aws_session_token: str = None,
17
region_name: str = None,
18
botocore_session = None,
19
profile_name: str = None,
20
aws_account_id: str = None
21
):
22
"""
23
Create a new aioboto3 session.
24
25
Parameters:
26
- aws_access_key_id: AWS access key ID
27
- aws_secret_access_key: AWS secret access key
28
- aws_session_token: AWS temporary session token
29
- region_name: Default region when creating new connections
30
- botocore_session: Use this aiobotocore session instead of creating new one
31
- profile_name: The name of a profile to use from AWS credentials
32
- aws_account_id: AWS account ID
33
"""
34
```
35
36
### Resource Creation
37
38
Creates AWS service resources using async context managers. Resources provide high-level interfaces to AWS services with object-oriented access patterns.
39
40
```python { .api }
41
def resource(
42
self,
43
service_name: str,
44
region_name: str = None,
45
api_version: str = None,
46
use_ssl: bool = True,
47
verify = None,
48
endpoint_url: str = None,
49
aws_access_key_id: str = None,
50
aws_secret_access_key: str = None,
51
aws_session_token: str = None,
52
config = None
53
):
54
"""
55
Create a service resource.
56
57
Parameters:
58
- service_name: The name of a service (e.g. 's3', 'dynamodb', 'ec2')
59
- region_name: The region to connect to
60
- api_version: The API version to use
61
- use_ssl: Whether to use SSL
62
- verify: Whether to verify SSL certificates
63
- endpoint_url: The complete URL to use for the constructed client
64
- aws_access_key_id: The access key to use for this resource
65
- aws_secret_access_key: The secret key to use for this resource
66
- aws_session_token: The session token to use for this resource
67
- config: Advanced configuration options
68
69
Returns:
70
ResourceCreatorContext: Async context manager that yields the service resource
71
"""
72
```
73
74
### Client Creation
75
76
Creates AWS service clients using async context managers. Clients provide low-level access to AWS service APIs with direct method calls.
77
78
```python { .api }
79
def client(
80
self,
81
service_name: str,
82
region_name: str = None,
83
api_version: str = None,
84
use_ssl: bool = True,
85
verify = None,
86
endpoint_url: str = None,
87
aws_access_key_id: str = None,
88
aws_secret_access_key: str = None,
89
aws_session_token: str = None,
90
config = None
91
):
92
"""
93
Create a low-level service client.
94
95
Parameters are the same as resource() method.
96
97
Returns:
98
Async context manager that yields the service client
99
"""
100
```
101
102
### Service Discovery
103
104
Methods for discovering available AWS services and resources supported by the session.
105
106
```python { .api }
107
def get_available_services(self) -> list:
108
"""
109
Get a list of service names that are available for creating clients.
110
111
Returns:
112
List of service names (strings)
113
"""
114
115
def get_available_resources(self) -> list:
116
"""
117
Get a list of resource names that are available for creating resources.
118
119
Returns:
120
List of resource names (strings)
121
"""
122
```
123
124
### Resource Context Management
125
126
The ResourceCreatorContext class manages the async lifecycle of AWS service resources.
127
128
```python { .api }
129
class ResourceCreatorContext:
130
async def __aenter__(self):
131
"""
132
Async context manager entry point.
133
134
Returns:
135
The configured service resource instance
136
"""
137
138
async def __aexit__(self, exc_type, exc, tb):
139
"""
140
Async context manager exit point. Properly closes the underlying client.
141
142
Parameters:
143
- exc_type: Exception type if an exception occurred
144
- exc: Exception value if an exception occurred
145
- tb: Traceback if an exception occurred
146
"""
147
```
148
149
## Usage Examples
150
151
### Basic Session Usage
152
153
```python
154
import aioboto3
155
156
# Create session with default credentials
157
session = aioboto3.Session()
158
159
# Create session with explicit credentials
160
session = aioboto3.Session(
161
aws_access_key_id='your-access-key',
162
aws_secret_access_key='your-secret-key',
163
region_name='us-east-1'
164
)
165
166
# Create session with profile
167
session = aioboto3.Session(profile_name='development')
168
```
169
170
### Using Resources
171
172
```python
173
async def use_dynamodb():
174
session = aioboto3.Session()
175
176
async with session.resource('dynamodb', region_name='us-east-1') as dynamodb:
177
table = await dynamodb.Table('my-table')
178
# Use the table...
179
```
180
181
### Using Clients
182
183
```python
184
async def use_s3_client():
185
session = aioboto3.Session()
186
187
async with session.client('s3', region_name='us-east-1') as s3:
188
response = await s3.list_buckets()
189
print(response['Buckets'])
190
```
191
192
### Service Discovery
193
194
```python
195
session = aioboto3.Session()
196
197
# List available services for clients
198
services = session.get_available_services()
199
print("Available services:", services)
200
201
# List available resources
202
resources = session.get_available_resources()
203
print("Available resources:", resources)
204
```