boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
9.06k stars 1.87k forks source link

Boto3 + Polly, silent failure without error #2413

Closed lvignals closed 4 years ago

lvignals commented 4 years ago

When using Boto3 to access Polly in the script below:

Code is as follow and taken from AWS website samples:

"""Getting Started Example for Python 2.7+/3.3+"""
"""Install Boto3 (AWS current Python SDK) before executing: $pip install boto3"""
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir

# Create a client using the credentials and region defined in the [adminuser] section of the AWS credentials file (~/.aws/credentials).
session = Session(profile_name="default") #changed adminuser to default (see ~/.aws/config file
polly = session.client("polly")

try: # Request speech synthesis
    response = polly.synthesize_speech(Text="Hello world!", OutputFormat="mp3", VoiceId="Joanna")
except (BotoCoreError, ClientError) as error: # The service returned an error, exit gracefully
    print(error)
    sys.exit(-1)

# Access the audio stream from the response
if "AudioStream" in response:
    # Note: Closing the stream is important because the service throttles on the number of parallel connections. Here we are using contextlib.closing to
    # ensure the close method of the stream object will be called automatically at the end of the with statement's scope.
        with closing(response["AudioStream"]) as stream:
            output = os.path.join(gettempdir(), "speech.mp3")
        try: # Open a file for writing the output as a binary stream
                with open(output, "wb") as file:
                    file.write(stream.read())
        except IOError as error: # Could not write to file, exit gracefully
            print(error)
            sys.exit(-1)
else: # The response didn't contain audio data, exit gracefully
    print("Could not stream audio")
    sys.exit(-1)

# Play the audio using the platform's default player
if sys.platform == "win32":
    os.startfile(output)
else: # The following works on macOS and Linux. (Darwin = mac, xdg-open = linux).
    opener = "open" if sys.platform == "darwin" else "mpg123" # "xdg-open" did not work it opened the file in text edit instead
    subprocess.call([opener, output])
swetashre commented 4 years ago

@lvignals - Thank you for your post. I am not able to reproduce the issue. Can you please run this code and provide me debug log ?

import boto3

polly = boto3.client('polly')

boto3.set_stream_logger('')

response = polly.synthesize_speech(Text="Hello world!", OutputFormat="mp3", VoiceId="Joanna")

 with closing(response["AudioStream"]) as stream:
     with open('polly.mp3', "wb") as fp:
         fp.write(stream.read())

Once the stream is read it will be closed. So please make sure you are not using same response stream twice.

lvignals commented 4 years ago

Actually I had found the issue, the construct that causes the empty file is caused by that line: with closing(response["AudioStream"]) as stream: changing to  with response["AudioStream"] as stream: and closing after (outside) the With statement gave me a complete audio file instead of a 0 byte file. I understand the reasoning behind using with closing(...)but it is not a reliable construct apparently. All the best and thank you for the reply. LV

On Wednesday, May 6, 2020, 04:21:28 PM PDT, swetashre <notifications@github.com> wrote:  

@lvignals - Thank you for your post. I am not able to reproduce the issue. Can you please run this code and provide me debug log ? import boto3

polly = boto3.client('polly')

boto3.set_stream_logger('')

response = polly.synthesize_speech(Text="Hello world!", OutputFormat="mp3", VoiceId="Joanna")

with closing(response["AudioStream"]) as stream: with open('polly.mp3', "wb") as fp: fp.write(stream.read()) Once the stream is read it will be closed. So please make sure you are not using same response stream twice.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub, or unsubscribe.

|

Boto3 + Polly, silent failure without error · Issue #2413 · boto/boto3

When using Boto3 to access Polly in the script below: Script runs without error but creates an empty .mp3 file. ... |

|

|

|

Build software better, together

GitHub is where people build software. More than 50 million people use GitHub to discover, fork, and contribute ... |

|

|

swetashre commented 4 years ago

I am glad you got it working. Closing this issue as it has been resolved. Please reopen if you have more concerns.