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-lexiconexample deletes the specified lexicon.aws polly delete-lexicon \ --namew3cThis 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-lexiconexample retrieves the content of the specified pronunciation lexicon.aws polly get-lexicon \ --namew3cOutput:
{ "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-taskexample retrieves information about the specified speech synthesis task.aws polly get-speech-synthesis-task \ --task-id70b61c0f-57ce-4715-a247-cae8729dcce9Output:
{ "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" } }For more information, see Creating long audio files
in the Amazon Polly Developer Guide. -
For API details, see GetSpeechSynthesisTask
in Amazon CLI Command Reference.
-
The following code example shows how to use list-lexicons.
- Amazon CLI
-
To list your lexicons
The following
list-lexiconsexample lists your pronunciation lexicons.aws polly list-lexiconsOutput:
{ "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-tasksexample lists your speech synthesis tasks.aws polly list-speech-synthesis-tasksOutput:
{ "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" } ] }For more information, see Creating long audio files
in the Amazon Polly Developer Guide. -
For API details, see ListSpeechSynthesisTasks
in Amazon CLI Command Reference.
-
The following code example shows how to use put-lexicon.
- Amazon CLI
-
To store a lexicon
The following
put-lexiconexample stores the specified pronunciation lexicon. Theexample.plsfile specifies a W3C PLS-compliant lexicon.aws polly put-lexicon \ --namew3c\ --contentfile://example.plsContents 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-taskexample synthesizes the text intext_file.txtand stores the resulting MP3 file in the specified bucket.aws polly start-speech-synthesis-task \ --output-formatmp3\ --output-s3-bucket-nameamzn-s3-demo-bucket\ --textfile://text_file.txt\ --voice-idJoannaOutput:
{ "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" } }For more information, see Creating long audio files
in the Amazon Polly Developer Guide. -
For API details, see StartSpeechSynthesisTask
in Amazon CLI Command Reference.
-