

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

# 使用 的 Amazon Polly 範例 AWS CLI
<a name="cli_polly_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Amazon Polly 執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `delete-lexicon`
<a name="polly_DeleteLexicon_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-lexicon`。

**AWS CLI**  
**刪除語彙**  
下列 `delete-lexicon` 範例會刪除指定的語彙。  

```
aws polly delete-lexicon \
    --name w3c
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[使用 DeleteLexicon 操作](https://docs.aws.amazon.com/polly/latest/dg/gs-delete-lexicon.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteLexicon](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/delete-lexicon.html)。

### `get-lexicon`
<a name="polly_GetLexicon_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-lexicon`。

**AWS CLI**  
**擷取語彙的內容**  
下列 `get-lexicon` 範例會擷取指定發音語彙的內容。  

```
aws polly get-lexicon \
    --name w3c
```
輸出：  

```
{
    "Lexicon": {
        "Content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<lexicon version=\"1.0\" \n      xmlns=    \"http://www.w3.org/2005/01/pronunciation-lexicon\"\n      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n          xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon \n        http://www.w3.org/TR/2007/CR-pronunciation-    lexicon-20071212/pls.xsd\"\n      alphabet=\"ipa\" \n      xml:lang=\"en-US\">\n  <lexeme>\n    <grapheme>W3C</grapheme>\n        <alias>World Wide Web Consortium</alias>\n  </lexeme>\n</lexicon>\n",
        "Name": "w3c"
    },
    "LexiconAttributes": {
        "Alphabet": "ipa",
        "LanguageCode": "en-US",
        "LastModified": 1603908910.99,
        "LexiconArn": "arn:aws:polly:us-west-2:880185128111:lexicon/w3c",
        "LexemesCount": 1,
        "Size": 492
    }
}
```
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[使用 GetLexicon 操作](https://docs.aws.amazon.com/polly/latest/dg/gs-get-lexicon.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetLexicon](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/get-lexicon.html)。

### `get-speech-synthesis-task`
<a name="polly_GetSpeechSynthesisTask_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-speech-synthesis-task`。

**AWS CLI**  
**取得語音合成任務的相關資訊**  
下列 `get-speech-synthesis-task` 範例會擷取指定語音合成任務的相關資訊。  

```
aws polly get-speech-synthesis-task \
    --task-id 70b61c0f-57ce-4715-a247-cae8729dcce9
```
輸出：  

```
{
    "SynthesisTask": {
        "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9",
        "TaskStatus": "completed",
        "OutputUri": "https://s3.us-west-2.amazonaws.com/amzn-s3-demo-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3",
        "CreationTime": 1603911042.689,
        "RequestCharacters": 1311,
        "OutputFormat": "mp3",
        "TextType": "text",
        "VoiceId": "Joanna"
    }
}
```
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[建立長時間音訊檔案](https://docs.aws.amazon.com/polly/latest/dg/longer-cli.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetSpeechSynthesisTask](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/get-speech-synthesis-task.html)。

### `list-lexicons`
<a name="polly_ListLexicons_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-lexicons`。

**AWS CLI**  
**列出語彙**  
下列 `list-lexicons` 範例會列出您的發音語彙。  

```
aws polly list-lexicons
```
輸出：  

```
{
    "Lexicons": [
        {
            "Name": "w3c",
            "Attributes": {
                "Alphabet": "ipa",
                "LanguageCode": "en-US",
                "LastModified": 1603908910.99,
                "LexiconArn": "arn:aws:polly:us-east-2:123456789012:lexicon/w3c",
                "LexemesCount": 1,
                "Size": 492
            }
        }
    ]
}
```
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[使用 ListLexicons 操作](https://docs.aws.amazon.com/polly/latest/dg/gs-list-lexicons.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListLexicons](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/list-lexicons.html)。

### `list-speech-synthesis-tasks`
<a name="polly_ListSpeechSynthesisTasks_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-speech-synthesis-tasks`。

**AWS CLI**  
**列出您的語音合成任務**  
下列 `list-speech-synthesis-tasks` 範例會列出您的語音合成任務。  

```
aws polly list-speech-synthesis-tasks
```
輸出：  

```
{
    "SynthesisTasks": [
        {
            "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9",
            "TaskStatus": "completed",
            "OutputUri": "https://s3.us-west-2.amazonaws.com/amzn-s3-demo-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3",
            "CreationTime": 1603911042.689,
            "RequestCharacters": 1311,
            "OutputFormat": "mp3",
            "TextType": "text",
            "VoiceId": "Joanna"
        }
    ]
}
```
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[建立長時間音訊檔案](https://docs.aws.amazon.com/polly/latest/dg/longer-cli.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListSpeechSynthesisTasks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/list-speech-synthesis-tasks.html)。

### `put-lexicon`
<a name="polly_PutLexicon_cli_topic"></a>

以下程式碼範例顯示如何使用 `put-lexicon`。

**AWS CLI**  
**存放語彙**  
下列 `put-lexicon` 範例存放指定的發音語彙。`example.pls` 檔案會指定 W3C PLS 相容語彙。  

```
aws polly put-lexicon \
    --name w3c \
    --content file://example.pls
```
`example.pls` 的內容  

```
{
    <?xml version="1.0" encoding="UTF-8"?>
    <lexicon version="1.0"
        xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
            http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
        alphabet="ipa"
        xml:lang="en-US">
        <lexeme>
            <grapheme>W3C</grapheme>
            <alias>World Wide Web Consortium</alias>
        </lexeme>
    </lexicon>
}
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[使用 PutLexicon 操作](https://docs.aws.amazon.com/polly/latest/dg/gs-put-lexicon.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [PutLexicon](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/put-lexicon.html)。

### `start-speech-synthesis-task`
<a name="polly_StartSpeechSynthesisTask_cli_topic"></a>

以下程式碼範例顯示如何使用 `start-speech-synthesis-task`。

**AWS CLI**  
**合成文字**  
下列 `start-speech-synthesis-task` 範例會在 `text_file.txt` 中合成文字，並將產生的 MP3 檔案存放在指定的儲存貯體中。  

```
aws polly start-speech-synthesis-task \
    --output-format mp3 \
    --output-s3-bucket-name amzn-s3-demo-bucket \
    --text  file://text_file.txt \
    --voice-id Joanna
```
輸出：  

```
{
    "SynthesisTask": {
        "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9",
        "TaskStatus": "scheduled",
        "OutputUri": "https://s3.us-east-2.amazonaws.com/amzn-s3-demo-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3",
        "CreationTime": 1603911042.689,
        "RequestCharacters": 1311,
        "OutputFormat": "mp3",
        "TextType": "text",
        "VoiceId": "Joanna"
    }
}
```
如需詳細資訊，請參閱《*Amazon Polly 開發人員指南*》中的[建立長時間音訊檔案](https://docs.aws.amazon.com/polly/latest/dg/longer-cli.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [StartSpeechSynthesisTask](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/polly/start-speech-synthesis-task.html)。