

 [適用於 JavaScript 的 AWS SDK V3 API 參考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)詳細說明 第 3 版 適用於 JavaScript 的 AWS SDK (V3) 的所有 API 操作。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon Polly 範例
<a name="polly-examples"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v3/developer-guide/images/nodeicon.png)

**這個 Node.js 程式碼範例會說明：**
+ 使用 Amazon Polly 將錄製的音訊上傳至 Amazon S3

## 案例
<a name="polly-example-synthesize-to-s3-scenario"></a>

在此範例中，一系列 Node.js 模組用於使用以下 Amazon S3 用戶端類別的方法，使用 Amazon Polly 自動將錄製的音訊上傳至 Amazon S3：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-polly/Class/StartSpeechSynthesisTaskCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-polly/Class/StartSpeechSynthesisTaskCommand/)

## 先決條件任務
<a name="polly-example-synthesize-to-s3-prerequisites"></a>

若要設定和執行此範例，您必須先完成這些任務：
+ 依照[ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/s3/README.md) 上的指示，設定專案環境以執行節點 JavaScript 範例。
+ 透過使用者登入資料建立共用組態檔。如需提供共用登入資料檔案的詳細資訊，請參閱 *AWS SDKs * [和工具參考指南中的共用組態和登入資料檔案](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)。
+ 建立 AWS Identity and Access Management (IAM) 未驗證的 Amazon Cognito 使用者角色輪詢：SynthesizeSpeech 許可，以及連接 IAM 角色的 Amazon Cognito 身分集區。以下[使用 建立 AWS 資源 CloudFormation](#polly-example-synthesize-to-s3-create-resources)章節說明如何建立這些資源。

**注意**  
此範例使用 Amazon Cognito，但如果您沒有使用 Amazon Cognito，您的 AWS 使用者必須擁有下列 IAM 許可政策  

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Action": [
        "mobileanalytics:PutEvents",
        "cognito-sync:*"
      ],
      "Resource": "*",
      "Effect": "Allow"
    },
    {
      "Action": "polly:SynthesizeSpeech",
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}
```

## 使用 建立 AWS 資源 CloudFormation
<a name="polly-example-synthesize-to-s3-create-resources"></a>

CloudFormation 可讓您以可預測且重複的方式建立和佈建 AWS 基礎設施部署。如需 的詳細資訊 CloudFormation，請參閱[AWS CloudFormation 《 使用者指南》](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/)。

若要建立 CloudFormation 堆疊：

1. 在 [AWS CLI 使用者指南](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)中安裝和設定 AWS CLI 下列指示。

1. 在專案資料夾的`setup.yaml`根目錄中建立名為 的檔案，並將 [ GitHub 上的此處](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/polly/general-examples/src/setup.yaml)內容複製到其中。
**注意**  
 CloudFormation 範本是使用 [ GitHub 上此處](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/resources/cdk/javascript_example_code_polly_aws_service/)提供的 AWS CDK 產生。如需 的詳細資訊 AWS CDK，請參閱 [AWS Cloud Development Kit (AWS CDK) 開發人員指南](https://docs.aws.amazon.com/cdk/latest/guide/)。

1. 從命令列執行下列命令，將 *STACK\$1NAME* 取代為堆疊的唯一名稱。
**重要**  
堆疊名稱在 AWS 區域和 AWS 帳戶中必須是唯一的。您最多可以指定 128 個字元，且允許使用數字和連字號。

   ```
   aws cloudformation create-stack --stack-name STACK_NAME --template-body file://setup.yaml --capabilities CAPABILITY_IAM
   ```

   如需`create-stack`命令參數的詳細資訊，請參閱 [AWS CLI 命令參考指南](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html)和 [CloudFormation 使用者指南](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html)。

1. 導覽至 CloudFormation 管理主控台，選擇 **Stacks**，選擇堆疊名稱，然後選擇**資源**索引標籤以檢視建立的資源清單。  
![\[CloudFormation 資源\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v3/developer-guide/images/cfn_polly.png)

## 使用 Amazon Polly 將錄製的音訊上傳至 Amazon S3
<a name="polly-example-synthesize-to-s3-example"></a>

以檔名 `polly_synthesize_to_s3.js` 建立一個 Node.js 模組。請務必如先前所示設定軟體開發套件，包括安裝所需的用戶端和套件。在程式碼中，輸入 *REGION* 和 *BUCKET\$1NAME*。若要存取 Amazon Polly，請建立`Polly`用戶端服務物件。`IdentityPoolId` 將 *"IDENTITY\$1POOL\$1ID"* 取代為您為此範例建立的 Amazon Cognito 身分集區**範例頁面**中的 。這也會傳遞給每個用戶端物件。

呼叫 Amazon Polly 用戶端服務物件的 `StartSpeechSynthesisCommand`方法合成語音訊息，並將其上傳至 Amazon S3 儲存貯體。

```
import { StartSpeechSynthesisTaskCommand } from "@aws-sdk/client-polly";
import { pollyClient } from "./libs/pollyClient.js";

// Create the parameters
const params = {
  OutputFormat: "mp3",
  OutputS3BucketName: "videoanalyzerbucket",
  Text: "Hello David, How are you?",
  TextType: "text",
  VoiceId: "Joanna",
  SampleRate: "22050",
};

const run = async () => {
  try {
    await pollyClient.send(new StartSpeechSynthesisTaskCommand(params));
    console.log(`Success, audio file added to ${params.OutputS3BucketName}`);
  } catch (err) {
    console.log("Error putting object", err);
  }
};
run();
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/polly/general-examples/src/polly_synthesize_to_s3.js)找到這個範本程式碼。