The Amazon SDK for JavaScript V3 API
            Reference Guide
Amazon Polly examples
         
             
         
         
    
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 Shared config and credentials files in the Amazon SDKs and Tools Reference Guide. 
- Create an Amazon Identity and Access Management (IAM) Unauthenticated Amazon Cognito user role polly:SynthesizeSpeech permissions, and an Amazon Cognito identity pool with the IAM role attached to it. The Create the Amazon resources using the Amazon CloudFormationsection below describes how to create these resources. 
Note
This example uses Amazon Cognito, but if you are not using Amazon Cognito then your Amazon user must have following IAM permissions policy
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 User Guide.
To create the Amazon CloudFormation stack:
- Install and configure the Amazon CLI following the instructions in the Amazon CLI User Guide - . 
- Create a file named - setup.yamlin 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. 
- Run the following command from the command line, replacing - STACK_NAMEwith 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-stackcommand parameters, see the Amazon CLI Command Reference guide- , and the Amazon CloudFormation User Guide - . 
- 
            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. 
import { StartSpeechSynthesisTaskCommand } from "@aws-sdk/client-polly"; import { pollyClient } from "./libs/pollyClient.js"; // Create the parameters const 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