Tono Nam
Tono Nam

Reputation: 36058

Fails to authenticate with Amazon Polly

I been trying to make a hello wold example on how to use polly (https://aws.amazon.com/polly/)

I do not want to download any libraries. I just want to make a simple http request with my credentials to amazon web serviecs and get back an audio. Is that possible?

I have already created a user on IAM and it looks like this:

enter image description here

This is what I have so far and it does NOT converts the text to speech. I think the problem is authenticating. :/

static void Main(string[] args)
    {
        var accessKeyId = @"I place in here my access key";
        var secretKey = @"I place in here my secret key";

        //Amazon.Runtime.AWSCredentials credentials = new 
        AmazonPollyClient client = new AmazonPollyClient(accessKeyId, secretKey);

        // Create describe voices request.
        DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest();
        // Synchronously ask Amazon Polly to describe available TTS voices.
        DescribeVoicesResponse describeVoicesResult = client.DescribeVoices(describeVoicesRequest);
        List<Voice> voices = describeVoicesResult.Voices;


        // Create speech synthesis request.
        SynthesizeSpeechRequest synthesizeSpeechPresignRequest = new SynthesizeSpeechRequest();
        // Text
        synthesizeSpeechPresignRequest.Text = "Hello world!";
        // Select voice for synthesis.
        synthesizeSpeechPresignRequest.VoiceId = voices[0].Id;
        // Set format to MP3.
        synthesizeSpeechPresignRequest.OutputFormat = OutputFormat.Mp3;
        // Get the presigned URL for synthesized speech audio stream.
        var presignedSynthesizeSpeechUrl = client.SynthesizeSpeechAsync(synthesizeSpeechPresignRequest).GetAwaiter().GetResult();
        using (FileStream output = File.OpenWrite("hello_world.mp3"))
        {
            presignedSynthesizeSpeechUrl.AudioStream.CopyTo(output);
        }

        Console.Read();
    }

Note for this example to compile I had to add the nuget package AWSSDK.Polly

Upvotes: 1

Views: 1473

Answers (1)

E.J. Brennan
E.J. Brennan

Reputation: 46859

You need to specify a region for you credentials call as a third parameter - i.e. RegionEndpoint.USEast1.

I.E:

AmazonPollyClient client = new AmazonPollyClient("AKI5ZLVN6QXO123OJA", "4sYnPuAzMk/k07JA456728VbTpX4F5/FAKEGDiAKm", RegionEndpoint.USEast1);

(It has nothing to do with it being a free account.)

Upvotes: 2

Related Questions