Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS polly sample example in python?

First Time I am trying AWS services. I have to integrate AWS polly with asterisk for text to speech.

here is example code i written to convert text to speech

from boto3 import  client
import boto3
import StringIO
from contextlib import closing

polly = client("polly", 'us-east-1' )
response = polly.synthesize_speech(
    Text="Good Morning. My Name is Rajesh. I am Testing Polly AWS Service For Voice Application.",
    OutputFormat="mp3",
    VoiceId="Raveena")

print(response)

if "AudioStream" in response:
    with closing(response["AudioStream"]) as stream:
        data = stream.read()
        fo = open("pollytest.mp3", "w+")
        fo.write( data )
        fo.close()

I am getting following error.

Traceback (most recent call last):
  File "pollytest.py", line 11, in <module>
    VoiceId="Raveena")
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 253, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 530, in _make_api_call
    operation_model, request_dict)
  File "/usr/local/lib/python2.7/dist-packages/botocore/endpoint.py", line 141, in make_request
    return self._send_request(request_dict, operation_model)
  File "/usr/local/lib/python2.7/dist-packages/botocore/endpoint.py", line 166, in _send_request
    request = self.create_request(request_dict, operation_model)
  File "/usr/local/lib/python2.7/dist-packages/botocore/endpoint.py", line 150, in create_request
    operation_name=operation_model.name)
  File "/usr/local/lib/python2.7/dist-packages/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/botocore/signers.py", line 90, in handler
    return self.sign(operation_name, request)
  File "/usr/local/lib/python2.7/dist-packages/botocore/signers.py", line 147, in sign
    auth.add_auth(request)
  File "/usr/local/lib/python2.7/dist-packages/botocore/auth.py", line 316, in add_auth
    raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

I want to provide credentials directly in this script so that i can use this in asterisk system application.

UPDATE: created a file ~/.aws/credentials with below content

[default]
aws_access_key_id=XXXXXXXX
aws_secret_access_key=YYYYYYYYYYY

now for my current login user its working fine, but for asterisk PBX it is not working.

like image 752
rajesh6115 Avatar asked Oct 30 '22 12:10

rajesh6115


1 Answers

Your code runs perfectly fine for me!

The last line is saying:

botocore.exceptions.NoCredentialsError: Unable to locate credentials

So, it is unable to authenticate against AWS.

If you are running this code on an Amazon EC2 instance, the simplest method is to assign an IAM Role to the instance when it is launched (it can't be added later). This will automatically assign credentials that can be used by application running on the instance -- no code changes required.

Alternatively, you could obtain an Access Key and Secret Key from IAM for your IAM User and store those credentials in a local file via the aws configure command.

It is bad practice to put credentials in source code, since they may become compromised.

See:

  • IAM Roles for Amazon EC2
  • Best Practices for Managing AWS Access Keys
like image 161
John Rotenstein Avatar answered Nov 11 '22 17:11

John Rotenstein