php - 适用于 PHP 的 AWS 开发工具包 等待转录作业,直到状态为“已完成”
问题描述
所以我一直在研究 Promises 和 Waiters,我想知道是否有办法启动转录作业,同时提供回调,所以它会打勾(定期自行检查)直到转录的结果是 COMPLETED
, 然后使用回调获取带有转录的 json 并将结果写入 db. 所以我所要做的就是开始工作并提供回调,服务员会定期阻塞线程并检查状态,允许我在两者之间抛出其他请求,而不是用while
循环来做这一切。
我尝试了此处提供的示例,但它只是使用该wait()
方法并阻塞线程,直到它得到结果。
甚至可以使用转录服务吗?非常感谢一个关于如何做到这一点的小代码示例!
解决方案
我通过执行以下操作从 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
推荐阅读
- dataframe - PySpark 动态爆炸嵌套列 - 从使用 FOR 更改为更优化的解决方案
- mysql - MySQL 日志记录使用触发器更改。有没有更好的办法?
- python - 给定一个排序数字列表,根据数字之间的距离划分为子列表
- javascript - 使用带有浏览器内跨域登录的 cypress 对 Auth0 进行身份验证
- xcode - Xcode 13 编辑器内联错误不会消失
- unicode - 希伯来字符 Javascript 上的 Unicode 组合
- java - Thymeleaf 片段处理器和 Spring 5
- java - 需要了解 logstash 和 elk
- azure - 如何获取 terraform 计划以在集群内创建私有 AKS 集群和命名空间?
- terraform - Terraform Remote StateFile 用于相同配置的多个实例