Amazon Polly examples - Amazon SDK for JavaScript
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.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Amazon Polly examples


            JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • Upload audio recorded using Amazon Polly to Amazon S3

The scenario

In this example, a series of Node.js modules are used to automatically upload audio recorded using Amazon Polly to Amazon S3 using these methods of the Amazon S3 client class:

Prerequisite tasks

To set up and run this example, you must first complete these tasks:

Note

This example uses Amazon Cognito, but if you are not using Amazon Cognito then your Amazon user must have following IAM permissions policy

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

Create the Amazon resources using the Amazon CloudFormation

Amazon CloudFormation enables you to create and provision Amazon infrastructure deployments predictably and repeatedly. For more information about Amazon CloudFormation, see the Amazon CloudFormation developer guide..

To create the Amazon CloudFormation stack:

  1. Install and configure the Amazon CLI following the instructions in the Amazon CLI User Guide.

  2. Create a file named setup.yaml in the root directory of your project folder, and copy the content here on GitHub into it.

    Note

    The Amazon CloudFormation template was generated using the Amazon CDK available here on GitHub. For more information about the Amazon CDK, see the Amazon Cloud Development Kit (Amazon CDK) Developer Guide.

  3. Run the following command from the command line, replacing STACK_NAME with a unique name for the stack.

    Important

    The stack name must be unique within an Amazon Region and Amazon account. You can specify up to 128 characters, and numbers and hyphens are allowed.

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

    For more information on the create-stack command parameters, see the Amazon CLI Command Reference guide, and the Amazon CloudFormation User Guide.

  4. Navigate to the Amazon CloudFormation management console, choose Stacks, choose the stack name, and choose the Resources tab to view a list of the created resources.

Upload audio recorded using Amazon Polly to Amazon S3

Create a Node.js module with the file name polly_synthesize_to_s3.js. Make sure to configure the SDK as previously shown, including installing the required clients and packages. In the code, enter the REGION, and the BUCKET_NAME. To access Amazon Polly, create an Polly client service object. Replace "IDENTITY_POOL_ID" with the IdentityPoolId from the Sample page of the Amazon Cognito identity pool you created for this example. This is also passed to each client object.

Call the StartSpeechSynthesisCommand method of the Amazon Polly client service object synthesize the voice message and upload it to the Amazon S3 bucket.

const { StartSpeechSynthesisTaskCommand } = require("@aws-sdk/client-polly"); const { pollyClient } = require("./libs/pollyClient.js"); // Create the parameters var 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();

This sample code can be found here on GitHub.