我第一次尝试 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/凭据
[default]
aws_access_key_id=XXXXXXXX
aws_secret_access_key=YYYYYYYYYYY
现在对于我当前的登录用户来说,它工作正常,但对于星号 PBX,它不起作用。
你的代码对我来说运行得很好!
最后一行是说:
botocore.exceptions.NoCredentialsError: Unable to locate credentials
因此,它无法针对 AWS 进行身份验证。
如果您在 Amazon EC2 实例上运行此代码,最简单的方法是在实例启动时为其分配 IAM 角色(以后无法添加(。这将自动分配实例上运行的应用程序可以使用的凭据 - 无需更改代码。
或者,您可以从 IAM 获取 IAM 用户的访问密钥和私有密钥,并通过 aws configure
命令将这些凭证存储在本地文件中。
将凭据放在源代码中是一种不好的做法,因为它们可能会受到损害。
看:
- 适用于 Amazon EC2 的 IAM 角色
- 管理 AWS 访问密钥的最佳实践
,星号pbx通常在星号用户下运行。
因此,您为该用户而不是root进行了身份验证。