$npx -y skills add aws/agent-toolkit-for-aws --skill aws-sdk-js-v3-usageAWS SDK for JavaScript v3 development patterns. Use when writing JavaScript or TypeScript code that uses AWS services via @aws-sdk/* packages (aws-sdk-js-v3), or when asked about schemas, runtime validation, serialization, or code generation in the context of the JS/TS AWS SDK.
| 1 | > Do not use emojis in any code, comments, or output when this skill is active. |
| 2 | |
| 3 | # AWS SDK for JavaScript v3 |
| 4 | |
| 5 | ## Package Structure |
| 6 | |
| 7 | - `@aws-sdk/client-*` — one per service, generated by [smithy-typescript](https://github.com/awslabs/smithy-typescript); one-to-one with AWS services and operations |
| 8 | - `@aws-sdk/lib-*` — higher-level helpers (e.g. `lib-dynamodb`, `lib-storage`) |
| 9 | - `@aws-sdk/*` (no prefix) — utility packages (mostly internal; don't import deep paths) |
| 10 | |
| 11 | Always import from the package root: |
| 12 | |
| 13 | ```js |
| 14 | import { S3Client } from "@aws-sdk/client-s3"; // correct |
| 15 | // NOT: import { S3Client } from "@aws-sdk/client-s3/dist-cjs/S3Client" |
| 16 | ``` |
| 17 | |
| 18 | ## Two Client Styles |
| 19 | |
| 20 | **Bare-bones** (preferred — smaller bundle): |
| 21 | |
| 22 | ```js |
| 23 | import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; |
| 24 | const client = new S3Client({ region: "us-east-1" }); |
| 25 | const output = await client.send(new GetObjectCommand({ Bucket: "b", Key: "k" })); |
| 26 | ``` |
| 27 | |
| 28 | **Aggregated** (v2-style but NOT v2, larger bundle): |
| 29 | |
| 30 | ```js |
| 31 | import { S3 } from "@aws-sdk/client-s3"; |
| 32 | const client = new S3({ region: "us-east-1" }); |
| 33 | const output = await client.getObject({ Bucket: "b", Key: "k" }); |
| 34 | ``` |
| 35 | |
| 36 | ## Client Configuration |
| 37 | |
| 38 | No global config in v3 — pass config to each client. `region` is always required; set it explicitly or via `AWS_REGION` env var. |
| 39 | |
| 40 | ```js |
| 41 | const config = { region: "us-east-1", maxAttempts: 5 }; |
| 42 | const s3 = new S3Client(config); |
| 43 | const dynamo = new DynamoDBClient(config); |
| 44 | ``` |
| 45 | |
| 46 | **Do not read or mutate `client.config` after instantiation** — it is a resolved form (e.g. `region` becomes an async function). See `references/effective-practices.md`. |
| 47 | |
| 48 | For HTTP handler (`NodeHttpHandler` from `@smithy/node-http-handler`), retry strategy, endpoint details, logging, FIPS, dual-stack, protocol selection, and S3-specific options → see `references/clients.md`. |
| 49 | |
| 50 | ## Credentials |
| 51 | |
| 52 | All providers from `@aws-sdk/credential-providers`. Credentials are lazy and cached per client until ~5 min before expiry. |
| 53 | |
| 54 | ```js |
| 55 | // Default chain (env → ini → IMDS/ECS) — use in most Node.js apps |
| 56 | const client = new S3Client({ credentials: fromNodeProviderChain() }); |
| 57 | |
| 58 | // Assume role (NOTE: fromTemporaryCredentials is correct for STS AssumeRole) |
| 59 | const client = new S3Client({ |
| 60 | credentials: fromTemporaryCredentials({ params: { RoleArn: "arn:aws:iam::123456789012:role/MyRole" } }), |
| 61 | }); |
| 62 | |
| 63 | // Named profile |
| 64 | const client = new S3Client({ profile: "my-profile" }); |
| 65 | ``` |
| 66 | |
| 67 | Share credentials and socket pool across multi-region clients: |
| 68 | |
| 69 | ```js |
| 70 | const east = new S3Client({ region: "us-east-1" }); |
| 71 | const { credentials, requestHandler } = east.config; |
| 72 | const west = new S3Client({ region: "us-west-2", credentials, requestHandler }); |
| 73 | ``` |
| 74 | |
| 75 | For all providers (Cognito, SSO, web identity, custom chains, STS region priority) → see `references/credentials.md`. |
| 76 | |
| 77 | ## Streams (e.g. S3 GetObject Body) |
| 78 | |
| 79 | **Always read or discard streaming responses** — unread streams leave sockets open (socket exhaustion): |
| 80 | |
| 81 | ```js |
| 82 | const { Body } = await client.send(new GetObjectCommand({ Bucket: "b", Key: "k" })); |
| 83 | const str = await Body.transformToString(); // read as string |
| 84 | const bytes = await Body.transformToByteArray(); // read as Uint8Array |
| 85 | // or discard: |
| 86 | await (Body.destroy?.() ?? Body.cancel?.()); |
| 87 | ``` |
| 88 | |
| 89 | Streams can only be read once. |
| 90 | |
| 91 | ## Paginators |
| 92 | |
| 93 | Use `paginate*` functions instead of manual token handling: |
| 94 | |
| 95 | ```js |
| 96 | import { DynamoDBClient, paginateListTables } from "@aws-sdk/client-dynamodb"; |
| 97 | |
| 98 | const client = new DynamoDBClient({}); |
| 99 | |
| 100 | const tableNames = []; |
| 101 | for await (const page of paginateListTables({ client }, {})) { |
| 102 | // page contains a single paginated output. |
| 103 | tableNames.push(...page.TableNames); |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | ## DynamoDB DocumentClient |
| 108 | |
| 109 | Use `@aws-sdk/lib-dynamodb` to work with native JS types instead of AttributeValues: |
| 110 | |
| 111 | ```js |
| 112 | import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 113 | import { DynamoDBDocumentClient, GetCommand, PutCommand } from "@aws-sdk/lib-dynamodb"; |
| 114 | |
| 115 | const client = DynamoDBDocumentClient.from(new DynamoDBClient({})); |
| 116 | await client.send(new PutCommand({ TableName: "T", Item: { id: "1", name: "Alice" } })); |
| 117 | const { Item } = await client.send(new GetCommand({ TableName: "T", Key: { id: "1" } })); |
| 118 | ``` |
| 119 | |
| 120 | For marshall options, large numbers (NumberValue), pagination, and aggregated client → see `references/dynamodb.md`. |
| 121 | |
| 122 | ## S3: Presigned URLs, Multipart Upload, Waiters |
| 123 | |
| 124 | ```js |
| 125 | // Presigned GET URL |
| 126 | import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; |
| 127 | const url = await getSignedUrl(client, new GetObjectCommand({ Bucket: "b", Key: "k" }), { expiresIn: 3600 }); |
| 128 | |
| 129 | // Multipart upload (large files / streams) |
| 130 | import { Upload } from "@aws-sdk/lib-storage"; |
| 131 | const upload = new Upload({ client, params: { Bucket: |