gpt4 book ai didi

amazon-web-services - python中的AWS polly示例示例?

转载 作者:行者123 更新时间:2023-12-04 21:07:30 24 4
gpt4 key购买 nike

我第一次尝试 AWS 服务。我必须将 AWS polly 与 Asterisk 集成以进行文本到语音转换。

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

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

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

更新:
创建了一个包含以下内容的文件 ~/.aws/credentials
[default]
aws_access_key_id=XXXXXXXX
aws_secret_access_key=YYYYYYYYYYY

现在对于我当前的登录用户它工作正常,但对于 asterisk PBX 它不起作用。

最佳答案

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

最后一行是说:

botocore.exceptions.NoCredentialsError: Unable to locate credentials

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

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

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

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

看:
  • IAM Roles for Amazon EC2
  • Best Practices for Managing AWS Access Keys
  • 关于amazon-web-services - python中的AWS polly示例示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105241/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com