Amazon Polly examples using Amazon CLI - Amazon Command Line Interface
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.

Amazon Polly examples using Amazon CLI

The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with Amazon Polly.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use delete-lexicon.

Amazon CLI

To delete a lexicon

The following delete-lexicon example deletes the specified lexicon.

aws polly delete-lexicon \ --name w3c

This command produces no output.

For more information, see Using the DeleteLexicon operation in the Amazon Polly Developer Guide.

  • For API details, see DeleteLexicon in Amazon CLI Command Reference.

The following code example shows how to use get-lexicon.

Amazon CLI

To retrieve the content of a lexicon

The following get-lexicon example retrieves the content of the specified pronunciation lexicon.

aws polly get-lexicon \ --name w3c

Output:

{ "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 } }

For more information, see Using the GetLexicon operation in the Amazon Polly Developer Guide.

  • For API details, see GetLexicon in Amazon CLI Command Reference.

The following code example shows how to use get-speech-synthesis-task.

Amazon CLI

To get information about a speech synthesis task

The following get-speech-synthesis-task example retrieves information about the specified speech synthesis task.

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

Output:

{ "SynthesisTask": { "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9", "TaskStatus": "completed", "OutputUri": "https://s3.us-west-2.amazonaws.com/my-s3-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3", "CreationTime": 1603911042.689, "RequestCharacters": 1311, "OutputFormat": "mp3", "TextType": "text", "VoiceId": "Joanna" } }

For more information, see Creating long audio files in the Amazon Polly Developer Guide.

The following code example shows how to use list-lexicons.

Amazon CLI

To list your lexicons

The following list-lexicons example lists your pronunciation lexicons.

aws polly list-lexicons

Output:

{ "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 } } ] }

For more information, see Using the ListLexicons operation in the Amazon Polly Developer Guide.

  • For API details, see ListLexicons in Amazon CLI Command Reference.

The following code example shows how to use list-speech-synthesis-tasks.

Amazon CLI

To list your speech synthesis tasks

The following list-speech-synthesis-tasks example lists your speech synthesis tasks.

aws polly list-speech-synthesis-tasks

Output:

{ "SynthesisTasks": [ { "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9", "TaskStatus": "completed", "OutputUri": "https://s3.us-west-2.amazonaws.com/my-s3-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3", "CreationTime": 1603911042.689, "RequestCharacters": 1311, "OutputFormat": "mp3", "TextType": "text", "VoiceId": "Joanna" } ] }

For more information, see Creating long audio files in the Amazon Polly Developer Guide.

The following code example shows how to use put-lexicon.

Amazon CLI

To store a lexicon

The following put-lexicon example stores the specified pronunciation lexicon. The example.pls file specifies a W3C PLS-compliant lexicon.

aws polly put-lexicon \ --name w3c \ --content file://example.pls

Contents of 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> }

This command produces no output.

For more information, see Using the PutLexicon operation in the Amazon Polly Developer Guide.

  • For API details, see PutLexicon in Amazon CLI Command Reference.

The following code example shows how to use start-speech-synthesis-task.

Amazon CLI

To synthesize text

The following start-speech-synthesis-task example synthesizes the text in text_file.txt and stores the resulting MP3 file in the specified bucket.

aws polly start-speech-synthesis-task \ --output-format mp3 \ --output-s3-bucket-name my-s3-bucket \ --text file://text_file.txt \ --voice-id Joanna

Output:

{ "SynthesisTask": { "TaskId": "70b61c0f-57ce-4715-a247-cae8729dcce9", "TaskStatus": "scheduled", "OutputUri": "https://s3.us-east-2.amazonaws.com/my-s3-bucket/70b61c0f-57ce-4715-a247-cae8729dcce9.mp3", "CreationTime": 1603911042.689, "RequestCharacters": 1311, "OutputFormat": "mp3", "TextType": "text", "VoiceId": "Joanna" } }

For more information, see Creating long audio files in the Amazon Polly Developer Guide.