Amazon Polly examples - AWS SDK for JavaScript

Help us improve the AWS SDK for JavaScript version 3 (V3) documentation by providing feedback using the Feedback link, or create an issue or pull request on GitHub.

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (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:

  • Set up a project environment to run Node JavaScript examples by following the instructions on GitHub.

  • Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading credentials in Node.js from the shared credentials file.

  • Create resources for the example using the template here on GitHub to create a AWS CDK stack using either the AWS Web Services Management Console or the AWS CLI. For instructions on how to modify the stack, or to delete the stack and its associated resources when you have finished the tutorial, see here on GitHub.

    Note

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

    This resulting AWS CloudFormation stack creates the following resources:

    • An AWS Identity and Access Management role with full access permissions to Amazon Polly.

    • An Amazon Cognito identity pool with the IAM role attached to it.

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.

Note

                    Identity pool ID

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

const { CognitoIdentityClient } = require("@aws-sdk/client-cognito-identity"); const { fromCognitoIdentityPool, } = require("@aws-sdk/credential-provider-cognito-identity"); const { Polly, StartSpeechSynthesisTaskCommand, } = require("@aws-sdk/client-polly"); // Set the AWS Region const REGION = "REGION"; //e.g. "us-east-1" // Create the parameters var s3Params = { OutputFormat: "mp3", OutputS3BucketName: "BUCKET_NAME", Text: "Hello David, How are you?", TextType: "text", VoiceId: "Joanna", SampleRate: "22050", }; // Create the Polly service client, assigning your credentials const polly = new Polly({ region: REGION, credentials: fromCognitoIdentityPool({ client: new CognitoIdentityClient({ region: REGION }), identityPoolId: "IDENTITY_POOL_ID", // IDENTITY_POOL_ID }), }); const run = async () => { try { const data = await polly.send( new StartSpeechSynthesisTaskCommand(s3Params) ); console.log("Audio file added to " + s3Params.OutputS3BucketName); } catch (err) { console.log("Error putting object", err); } }; run();

This sample code can be found here on GitHub.