

 [AWS SDK for JavaScript V3 API リファレンスガイド](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)では、 AWS SDK for JavaScript バージョン3 (V3) のすべての API オペレーションについて詳しく説明します。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# サービスオブジェクトを作成して呼び出す
<a name="creating-and-calling-service-objects"></a>

JavaScript API は、利用可能なほとんどの AWS サービスをサポートしています。JavaScript APIの各サービスは、サービスがサポートするすべてのAPIを呼び出すために使用する`send`メソッドをクライアントクラスに提供します。JavaScript API のサービスクラス、オペレーション、およびパラメータの詳細については、[[ API Reference ]](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-transcribe/)を参照してください。

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"
});
```

## サービスオブジェクトのパラメータを指定する
<a name="specifying-service-object-parameters"></a>

サービスオブジェクトのメソッドを呼び出す場合、API の必要に応じて JSON でパラメータを渡します。例えば、Amazon S3 では、指定されたバケットとキーのオブジェクトを取得するために、`S3Client` から `GetObjectCommand` メソッドに以下のパラメータを渡します。JSON パラメータを渡す詳細については、「[JSON を使用する](working-with-json.md)」を参照してください。

```
s3Client.send(new GetObjectCommand({Bucket: 'bucketName', Key: 'keyName'}));
```

Amazon S3 パラメータの詳細については、API リファレンスの「[@aws-sdk/client-s3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-s3/)」を参照してください。

## TypeScript で生成されたクライアントに @smithy/types を使用する
<a name="smithy-types"></a>

TypeScript を使用している場合、`@smithy/types` パッケージを使用すると、クライアントの入出力シェイプを操作できます。

### シナリオ: 入力構造と出力構造から `undefined` を削除する
<a name="remove-undefined-from-input"></a>

生成されたシェイプのメンバーは、入力シェイプの場合は `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 タイプを絞り込む
<a name="remove-undefined-from-input"></a>

このシナリオは、 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!;
```