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

时间:2017-06-12 16:43:39

标签: javascript php amazon-web-services amazon-s3 amazon-polly

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 (Aws\S3\Exception\S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }

}

Polly response:

Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [AudioStream] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #264
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream: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
                                        (
                                        )

                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

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

所以好像我试图将整个对象上传到S3。上面的一行让我可以正确上传音频流。

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 (Aws\S3\Exception\S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }

}

答案 1 :(得分:0)

另一种方法是

$text = "Hi my name is Bob";
$format = 'mp3'; //json|mp3|ogg_vorbis|pcm
$voice = 'Joanna';
$result = $PollyClient->synthesizeSpeech([
'Text' => $text,
'OutputFormat' => $format,

'VoiceId' => $voice,
]);
$resultData = $result->get('AudioStream')->getContents();
#Save MP3 to S3
$s3bucket = 'bucket-name';
$filename = "message-".$id.'.mp3';
$result_s3 = $S3Client->putObject([
'Key'         => $filename,
'ACL'         => 'public-read',
'Body'        => $resultData,
'Bucket'      => $s3bucket,
'ContentType' => 'audio/mpeg',
'SampleRate'  => '8000'
]);
return $result_s3['ObjectURL'];