SDK v3 Fundamentals Basics
Seven examples to get you productive with the AWS SDK for JavaScript v3 - five basic and two intermediate.
Busque em todas as páginas da documentação
Seven examples to get you productive with the AWS SDK for JavaScript v3 - five basic and two intermediate.
Every example is pure TypeScript, because v3's command pattern, per-service packages, and credential providers have no boto3 equivalent. This is a tour of the SDK itself, not a cross-language comparison.
npm install @aws-sdk/client-s3. There is no single meta-package to install.aws configure, environment variables, or an IAM role. The default provider chain finds them automatically.AWS_REGION or passed per client as { region: "us-east-1" }.Each service is its own package, so you install and import exactly the client you need.
// Terminal: npm install @aws-sdk/client-s3
import { S3Client } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: "us-east-1" });
console.log(s3.constructor.name); // S3Client@aws-sdk/client-<service>, lowercase, hyphenated.AWS_REGION, so new S3Client({}) works when it is set.Related: Installing Only the Clients You Need - the per-service packaging model.
You never call a method named after the operation; you send a command object.
import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const response = await s3.send(new ListBucketsCommand({}));
for (const bucket of response.Buckets ?? []) {
console.log(bucket.Name);
}ListBucketsCommand) alongside the client.client.send(new XCommand(input)) is the universal calling convention in v3.undefined, so guard with ?? [] before iterating.Related: SDK v3's Command Pattern - why every call is a command.
Most commands take an input object, and the response is fully typed.
import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const head = await s3.send(new HeadObjectCommand({
Bucket: "my-bucket",
Key: "report.pdf",
}));
console.log(head.ContentLength, head.ContentType);HeadObject returns metadata only, without downloading the object body.undefined, so the compiler nudges you to handle absence.@types install is needed.AWS calls fail as typed exception classes you can match precisely.
import { S3Client, GetObjectCommand, NoSuchKey } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
try {
await s3.send(new GetObjectCommand({ Bucket: "my-bucket", Key: "missing.txt" }));
} catch (err) {
if (err instanceof NoSuchKey) console.log("object not found");
else throw err;
}NoSuchKey.instanceof rather than string-comparing error messages.err.$metadata.requestId; log it for AWS support cases.Related: SDK v3 Fundamentals Best Practices - error and retry habits.
Usually the default chain is enough, but you can pass a provider when you need control.
import { S3Client } from "@aws-sdk/client-s3";
import { fromIni } from "@aws-sdk/credential-providers";
const s3 = new S3Client({
region: "us-east-1",
credentials: fromIni({ profile: "staging" }),
});@aws-sdk/credential-providers is a separate install from the client package.fromIni({ profile }) reads a named profile from your shared config files.credentials entirely to let the default provider chain resolve them.Related: Credential Providers: fromEnv, fromIni, fromNodeProviderChain.
List operations return one page at a time; the paginate* helpers walk them for you.
import { S3Client, paginateListObjectsV2 } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const pages = paginateListObjectsV2(
{ client: s3 },
{ Bucket: "my-bucket", Prefix: "logs/" },
);
for await (const page of pages) {
for (const obj of page.Contents ?? []) console.log(obj.Key, obj.Size);
}paginate<Operation> from the same client package as an async generator.ContinuationToken bookkeeping the raw API requires.Prefix to fetch only what you need.for await...of consumes the pages lazily, one round trip at a time.A single service client sends every command for that service; reuse it, especially in Lambda.
import { DynamoDBClient, GetItemCommand, PutItemCommand } from "@aws-sdk/client-dynamodb";
// Module scope: constructed once, reused by every warm Lambda invocation.
const ddb = new DynamoDBClient({ region: "us-east-1" });
export async function handler(id: string) {
await ddb.send(new PutItemCommand({
TableName: "Visits",
Item: { id: { S: id }, count: { N: "1" } },
}));
return ddb.send(new GetItemCommand({ TableName: "Visits", Key: { id: { S: id } } }));
}DynamoDBClient sends both PutItemCommand and GetItemCommand.Related: SDK v3 Fundamentals Best Practices - client lifecycle rules.
Stack versions: This page was written for the AWS SDK for JavaScript v3 on Node.js 18+ (and boto3 1.43.x / Python 3.10+ where contrasted).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026