AWS使用PHP API转录:节流异常:超出速率

时间:2019-04-03 02:09:19

标签: php amazon-web-services aws-transcribe

尝试通过以下方式使用AWS开发工具包PHP API转录存储在我可以访问的S3存储桶中的音频文件,这给了我一个限制的例外:超出了速率。

我已经阅读了十多次文档,却找不到一个简单的示例来说明如何使用AWS transcribe及其PHP API成功地转录文件。

date_default_timezone_set('America/New_York');
try 
{
    require '/var/www/html/aws/sdk/aws-autoloader.php';
} 
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
use Aws\TranscribeService\TranscribeServiceClient;

$client = new Aws\TranscribeService\TranscribeServiceClient([
    'version'       => 'latest',
    'region'        => 'us-east-1',
    'credentials'   => [
                    'key'           => 'xxxx',
                    'secret'        => 'yyyy',
                    'curl.options'  => array(CURLOPT_VERBOSE => true)
                    ]
        ]);
$job_name = "tjob".date("mdyhisa");
$job_uri = "https://s3.amazonaws.com/....mp3";          

$result = $client->startTranscriptionJob([
    'LanguageCode' => 'en-US', 
    'Media' => [ 
    'MediaFileUri' => "$job_uri",
    ],
    'MediaFormat' => 'mp3', 

    'TranscriptionJobName' => "$job_name", 
]);
/* removing this loop and the sleep() below would retrieve some structured response, 
but of course the operation status is IN_PROGRESS */
while(true)
{
    /* added to discover if holding a few seconds would work: it doesn't
       and gives back a 504 Gateway Timeout */
    sleep(rand(3,5));
    /* -- */
    $result = $client->getTranscriptionJob(['TranscriptionJobName' => "$job_name"]);
    if ( ($result['TranscriptionJob']['TranscriptionJobStatus']=='COMPLETED') || ($result['TranscriptionJob']['TranscriptionJobStatus']=='FAILED'))
    {
        break;  
    }
}
var_dump($result);

问题是:如何获得转录输出?

顺便说一句,我不需要异步的……对于我的小项目来说,等待它处理并返回就可以了。

1 个答案:

答案 0 :(得分:0)

您的代码可能工作正常,但是您的while(true)循环调用API的次数过多,因此出现了throttling exception: rate exceeded错误。

我建议您在每次致电getTranscriptionJob的间隔之间要延迟5秒。我发现一项工作可能需要60秒钟才能完成,因此您无需连续调用它。