首页 > 解决方案 > 适用于 PHP 的 AWS 开发工具包 等待转录作业,直到状态为“已完成”

问题描述

所以我一直在研究 Promises 和 Waiters,我想知道是否有办法启动转录作业,同时提供回调,所以它会打勾(定期自行检查)直到转录的结果是 COMPLETED, 然后使用回调获取带有转录的 json 并将结果写入 db. 所以我所要做的就是开始工作并提供回调,服务员会定期阻塞线程并检查状态,允许我在两者之间抛出其他请求,而不是用while循环来做这一切。

我尝试了此处提供的示例,但它只是使用该wait()方法并阻塞线程,直到它得到结果。

甚至可以使用转录服务吗?非常感谢一个关于如何做到这一点的小代码示例!

标签: phpamazon-web-servicesasynchronouspromiseaws-transcribe

解决方案


我通过执行以下操作从 AWS Transcribe 获得了回调响应:

我使用 PHP SDK 创建了一个转录作业:

use Aws\TranscribeService\TranscribeServiceClient;
...

$transcriber = new TranscribeServiceClient([<config>]);
$job = $transcriber->startTranscriptionJob([<config>, 'TranscriptionJobName' => 'unique-job-name']);

接下来,我登录 AWS 控制台并转到 AWS Lambda。在 Lambda 中,我使用 Node.js 8.10 运行时创建了一个函数:

var https = require('https');

exports.handler = function(event, context) {

    var body='';

    // the post options
    var optionspost = {
        host: 'example.com',
        path: '/transcriptions/callback',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        }
    };

    var reqPost = https.request(optionspost, function(res) {
        res.on('data', function (chunk) {
            body += chunk;
        });

        context.succeed(body);
   });

   reqPost.write(JSON.stringify(event));
   reqPost.end();
};

这会发送一个 POST 请求https://<host><path>,并将event数据作为正文

接下来进入 AWS Cloudwatch 并创建一个rule Events->rules. 在Event Source - Service Name选择Transcribe和配置您的选项下(Event Type -> Transcribe Job State Change, Specific Status -> Completed)。在Targets选择下Lambda Function,然后选择您的function

Lambda Function这将在完成后触发对您的呼叫Transcribe Job。发布到您的服务器的Lambda Function帖子,其中包含转录作业的详细信息,包括Job Name: unique-job-name.

此时,您可以返回Cloudwatch并:

  • 选择您的Rule并单击show metrics for this rule
  • 选择Logs查看您的任何日志记录信息Lambda Function

推荐阅读