小编ame*_*dev的帖子

类型错误:<botocore.response.StreamingBody 对象位于 0x7f81898af438> 不可 JSON 序列化

我正在尝试使用 Python 创建一个AWS Lambda函数,该函数应从某些 API 接收文本并返回包含AudioStream对象的 JSON。为此,我使用AWS Polly。目前我可以AudioStream在我的机器上从AWS获取它并且工作正常。

为了使用 AWS Polly,我创建了一个特殊用户并授予他AmazonPollyReadOnlyAccess访问AmazonPollyFullAccess权限。

import boto3,json

polly= boto3.client('polly')
text='<speak>hey there wassup </speak>'

spoken_text=polly.synthesize_speech(Text=text,OutputFormat='mp3',VoiceId='Aditi',TextType='ssml')

newdata=json.dumps(spoken_text)
# return newdata
print(type(spoken_text))
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用此代码片段以 JSON 格式返回响应时,我收到错误。

/usr/bin/python3.5 /talker/aditi.py
Traceback (most recent call last):
  File "/talker/aditi.py", line 9, in <module>
    newdata=json.dumps(spoken_text)
  File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, …
Run Code Online (Sandbox Code Playgroud)

json amazon-web-services boto3 python-3.5 amazon-polly

7
推荐指数
2
解决办法
1万
查看次数