5

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.

rajesh6115
  • 705
  • 9
  • 21

2 Answers2

2

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:

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • i have created ~/.aws/credentials file and provided credential in default section. now its working for my user. But for asterisk PBX it's giving error. – rajesh6115 Feb 08 '17 at 10:43
  • What do you mean by "But for asterisk PBX it's giving error"? Does the PBX run under a different user account. In that case, create a credentials file in that user's home directory, too. Worst case you can put credentials in the code (use `aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY` when creating the `client()`), but that's a very insecure method. – John Rotenstein Feb 08 '17 at 10:45
  • Thanks I user configuration file in Python and kept credentials in config file. Also successfully integrated with asterisk PBX also. – rajesh6115 Feb 08 '17 at 15:26
0

Please note,asterisk pbx usually run under asterisk user.

So you have put authentification for that user, not root.

arheops
  • 15,544
  • 1
  • 21
  • 27