AWS SDK for JavaScript V3 API リファレンスガイドでは、 AWS SDK for JavaScript バージョン3 (V3) のすべての API オペレーションについて詳しく説明します。
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
サービスオブジェクトを作成して呼び出す
JavaScript API は、利用可能なほとんどの AWS のサービスをサポートしています。JavaScript APIの各サービスは、サービスがサポートするすべてのAPIを呼び出すために使用するsendメソッドをクライアントクラスに提供します。JavaScript API のサービスクラス、オペレーション、およびパラメータの詳細については、[ API Reference ]を参照してください。
Node.jsでSDKを使用する場合は、importを使用して、必要な各サービスのSDKパッケージをアプリケーションに追加します。これにより、現在のすべてのサービスがサポートされます。次の例では、us-west-1地域に Amazon S3 サービスオブジェクトを作成します。
// Import the Amazon S3 service client import { S3Client } from "@aws-sdk/client-s3"; // Create an S3 client in the us-west-1 Region const s3Client = new S3Client({ region: "us-west-1" });
サービスオブジェクトのパラメータを指定する
サービスオブジェクトのメソッドを呼び出す場合、API の必要に応じて JSON でパラメータを渡します。例えば、Amazon S3 では、指定されたバケットとキーのオブジェクトを取得するために、S3Client から GetObjectCommand メソッドに以下のパラメータを渡します。JSON パラメータを渡す詳細については、「JSON を使用する」を参照してください。
s3Client.send(new GetObjectCommand({Bucket: 'bucketName', Key: 'keyName'}));
Amazon S3 パラメータの詳細については、API リファレンスの「@aws-sdk/client-s3」を参照してください。
TypeScript で生成されたクライアントに @smithy/types を使用する
TypeScript を使用している場合、@smithy/types パッケージを使用すると、クライアントの入出力シェイプを操作できます。
シナリオ: 入力構造と出力構造から undefined を削除する
生成されたシェイプのメンバーは、入力シェイプの場合は undefined とのユニオン型になり、出力シェイプの場合は ? (オプション) になります。入力の場合、検証はサービス側で処理されます。出力の場合、実行時に出力データを検証することを強くお勧めします。
これらのステップをスキップする場合は、AssertiveClient または UncheckedClient の型ヘルパーを使用します。次の例では、Amazon S3 サービスで 型ヘルパーを使用します。
import { S3 } from "@aws-sdk/client-s3"; import type { AssertiveClient, UncheckedClient } from "@smithy/types"; const s3a = new S3({}) as AssertiveClient<S3>; const s3b = new S3({}) as UncheckedClient<S3>; // AssertiveClient enforces required inputs are not undefined // and required outputs are not undefined. const get = await s3a.getObject({ Bucket: "", // @ts-expect-error (undefined not assignable to string) Key: undefined, }); // UncheckedClient makes output fields non-nullable. // You should still perform type checks as you deem // necessary, but the SDK will no longer prompt you // with nullability errors. const body = await ( await s3b.getObject({ Bucket: "", Key: "", }) ).Body.transformToString();
非集約クライアントで Command 構文を使用して変換を使用する場合、以下の例に示すように、入力は別のクラスを通過するため、検証できません。
import { S3Client, ListBucketsCommand, GetObjectCommand, GetObjectCommandInput } from "@aws-sdk/client-s3"; import type { AssertiveClient, UncheckedClient, NoUndefined } from "@smithy/types"; const s3 = new S3Client({}) as UncheckedClient<S3Client>; const list = await s3.send( new ListBucketsCommand({ // command inputs are not validated by the type transform. // because this is a separate class. }) ); /** * Although less ergonomic, you can use the NoUndefined<T> * transform on the input type. */ const getObjectInput: NoUndefined<GetObjectCommandInput> = { Bucket: "undefined", // @ts-expect-error (undefined not assignable to string) Key: undefined, // optional params can still be undefined. SSECustomerAlgorithm: undefined, }; const get = s3.send(new GetObjectCommand(getObjectInput)); // outputs are still transformed. await get.Body.TransformToString();
シナリオ: Smithy-TypeScript で生成されたクライアントの出力ペイロード BLOB タイプを絞り込む
このシナリオは、主に AWS SDK for JavaScript v3 の S3Client 内など、ストリーミング本文を使用する操作に関連しています。
BLOB ペイロードタイプはプラットフォームに依存するため、クライアントが特定の環境で実行されていることをアプリケーションで指定することが必要な場合があります。これにより、次の例に示すように BLOB ペイロードタイプが絞り込まれます。
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3"; import type { NodeJsClient, SdkStream, StreamingBlobPayloadOutputTypes } from "@smithy/types"; import type { IncomingMessage } from "node:http"; // default client init. const s3Default = new S3Client({}); // client init with type narrowing. const s3NarrowType = new S3Client({}) as NodeJsClient<S3Client>; // The default type of blob payloads is a wide union type including multiple possible // request handlers. const body1: StreamingBlobPayloadOutputTypes = (await s3Default.send(new GetObjectCommand({ Key: "", Bucket: "" }))) .Body!; // This is of the narrower type SdkStream<IncomingMessage> representing // blob payload responses using specifically the node:http request handler. const body2: SdkStream<IncomingMessage> = (await s3NarrowType.send(new GetObjectCommand({ Key: "", Bucket: "" }))) .Body!;