|
47 | 47 | TranscriptResultStream, |
48 | 48 | ) |
49 | 49 | from smithy_aws_core.identity import ( |
| 50 | + AWSCredentialsIdentity, |
50 | 51 | ContainerCredentialsResolver, |
51 | 52 | EnvironmentCredentialsResolver, |
52 | 53 | IMDSCredentialsResolver, |
@@ -195,9 +196,22 @@ async def _run(self) -> None: |
195 | 196 | while True: |
196 | 197 | config_kwargs: dict[str, Any] = {"region": self._opts.region} |
197 | 198 | if self._credentials: |
198 | | - config_kwargs["aws_access_key_id"] = self._credentials.access_key_id |
199 | | - config_kwargs["aws_secret_access_key"] = self._credentials.secret_access_key |
200 | | - config_kwargs["aws_session_token"] = self._credentials.session_token |
| 199 | + # Use a credentials resolver for explicit credentials |
| 200 | + # for some reason, Config with direct values doesn't work |
| 201 | + class StaticCredsResolver: |
| 202 | + def __init__(self, creds: Credentials): |
| 203 | + self._identity = AWSCredentialsIdentity( |
| 204 | + access_key_id=creds.access_key_id, |
| 205 | + secret_access_key=creds.secret_access_key, |
| 206 | + session_token=creds.session_token, |
| 207 | + ) |
| 208 | + |
| 209 | + async def get_identity(self, **kwargs: Any) -> AWSCredentialsIdentity: |
| 210 | + return self._identity |
| 211 | + |
| 212 | + config_kwargs["aws_credentials_identity_resolver"] = StaticCredsResolver( |
| 213 | + self._credentials |
| 214 | + ) |
201 | 215 | else: |
202 | 216 | config_kwargs["aws_credentials_identity_resolver"] = ChainedIdentityResolver( |
203 | 217 | resolvers=( |
@@ -229,6 +243,8 @@ async def _run(self) -> None: |
229 | 243 | } |
230 | 244 | filtered_config = {k: v for k, v in live_config.items() if v and is_given(v)} |
231 | 245 |
|
| 246 | + tasks: list[asyncio.Task[Any]] = [] |
| 247 | + |
232 | 248 | try: |
233 | 249 | stream = await client.start_stream_transcription( |
234 | 250 | input=StartStreamTranscriptionInput(**filtered_config) |
@@ -286,14 +302,15 @@ async def handle_transcript_events( |
286 | 302 | else: |
287 | 303 | raise e |
288 | 304 | finally: |
289 | | - # Close input stream first |
290 | | - await utils.aio.gracefully_cancel(tasks[0]) |
291 | | - |
292 | | - # Wait for output stream to close cleanly |
293 | | - try: |
294 | | - await asyncio.wait_for(tasks[1], timeout=3.0) |
295 | | - except (asyncio.TimeoutError, asyncio.CancelledError): |
296 | | - await utils.aio.gracefully_cancel(tasks[1]) |
| 305 | + if tasks: |
| 306 | + # Close input stream first |
| 307 | + await utils.aio.gracefully_cancel(tasks[0]) |
| 308 | + |
| 309 | + # Wait for output stream to close cleanly |
| 310 | + try: |
| 311 | + await asyncio.wait_for(tasks[1], timeout=3.0) |
| 312 | + except (asyncio.TimeoutError, asyncio.CancelledError): |
| 313 | + await utils.aio.gracefully_cancel(tasks[1]) |
297 | 314 |
|
298 | 315 | # Ensure gather future is retrieved to avoid "exception never retrieved" |
299 | 316 | with contextlib.suppress(Exception): |
|
0 commit comments