Skip to content
Advertisement

s3 uploads audio file but it doesn’t play. How to upload audio stream to s3?

I’m trying to upload my Amazon Polly speech files to s3. They upload successfully, so no errors that I can work with but they don’t play.

I have an array of objects which include lyrics which are strings. I loop them and create a mp3 file then upload to s3.


Data structure:

Array
(
    [0] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

    [1] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

    [2] => stdClass Object
        (
            [lyrics] => sample lyrics

        )

)

.
Polly and S3 Function:

foreach($final as $key=>$f){

    $pollySpeech = $polly->synthesizeSpeech([
        'OutputFormat' => 'mp3',
        'Text' => $f->lyrics,
        'TextType' => 'text',
        'VoiceId' => 'Salli',
    ]);

    print_r($pollySpeech);

    try {
        $s3->putObject([
                'Bucket' => 'testbucket'
                'Key'    => $key.'.mp3',
                'Body'   => $pollySpeech,
                'ContentType' => 'audio/mpeg',
            'ACL'    => 'public-read',
        ]);
    } catch (AwsS3ExceptionS3Exception $e) {
        echo "There was an error uploading the file.n";
    }

}

Polly response:

AwsResult Object
(
    [data:AwsResult:private] => Array
        (
            [AudioStream] => GuzzleHttpPsr7Stream Object
                (
                    [stream:GuzzleHttpPsr7Stream:private] => Resource id #264
                    [size:GuzzleHttpPsr7Stream:private] => 
                    [seekable:GuzzleHttpPsr7Stream:private] => 1
                    [readable:GuzzleHttpPsr7Stream:private] => 1
                    [writable:GuzzleHttpPsr7Stream:private] => 1
                    [uri:GuzzleHttpPsr7Stream:private] => php://temp
                    [customMetadata:GuzzleHttpPsr7Stream:private] => Array
                        (
                        )

                )

            [ContentType] => audio/mpeg
            [RequestCharacters] => 90
            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://polly.eu-west-1.amazonaws.com/v1/speech
                    [headers] => Array
                        (
                            [x-amzn-requestid] => fc1a7ebf-4f8c-11e7-a1a3-555e1409e93f
                            [x-amzn-requestcharacters] => 90
                            [content-type] => audio/mpeg
                            [transfer-encoding] => chunked
                            [date] => Mon, 12 Jun 2017 16:34:20 GMT
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

Advertisement

Answer

$pollySpeech->get('AudioStream')->getContents();

So it seems like I tried to upload the whole object to S3. The line above lets me upload the audio stream properly.

foreach($final as $key=>$f){

    $pollySpeech = $polly->synthesizeSpeech([
        'OutputFormat' => 'mp3',
        'Text' => $f->lyrics,
        'TextType' => 'text',
        'VoiceId' => 'Salli',
    ]);

    print_r($pollySpeech);

    try {
        $s3->putObject([
                'Bucket' => 'testbucket'
                'Key'    => $key.'.mp3',
                'Body'   =>  $pollySpeech->get('AudioStream')->getContents(),
                'ContentType' => 'audio/mpeg',
            'ACL'    => 'public-read',
        ]);
    } catch (AwsS3ExceptionS3Exception $e) {
        echo "There was an error uploading the file.n";
    }

}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement