5

我第一次尝试 AWS 服务。我必须将 AWS polly 与星号集成以实现文本到语音。

这是我编写的将文本转换为语音的示例代码

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()

我收到以下错误。

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

我想直接在这个脚本中提供凭据,以便我可以在星号系统应用程序中使用它。

更新: 创建了一个具有以下内容的文件 ~/.aws/credentials

[default]
aws_access_key_id=XXXXXXXX
aws_secret_access_key=YYYYYYYYYYY

现在对于我当前的登录用户来说它工作正常,但对于星号 PBX 它不工作。

4

2 回答 2

2

你的代码对我来说运行得很好!

最后一行是说:

botocore.exceptions.NoCredentialsError: Unable to locate credentials

因此,它无法针对 AWS 进行身份验证。

如果您在 Amazon EC2 实例上运行此代码,最简单的方法是在实例启动时为其分配 IAM 角色(以后无法添加)。这将自动分配可供在实例上运行的应用程序使用的凭据——无需更改代码。

或者,您可以从 IAM 为您的 IAM 用户获取访问密钥和秘密密钥,并通过aws configure命令将这些凭证存储在本地文件中。

将凭据放在源代码中是不好的做法,因为它们可能会受到损害。

看:

于 2017-02-08T06:42:26.627 回答
0

请注意,asterisk pbx 通常在 asterisk 用户下运行。

因此,您已经为该用户而不是 root 进行了身份验证。

于 2017-02-08T12:51:40.367 回答