带有Http / 2的Node.js中的Amazon Transcribe流服务请求未响应

Man*_*noj 12 amazon-web-services node.js aws-transcribe

我试图将Amazon Transcribe Streaming Service与来自Node.js的http2请求一起使用,这是我遵循Streaming request格式的文档链接 。根据此文档,端点为https:// transcribe-streaming。<'region'>。amazonaws.com,但对此URL进行请求将导致未找到URL错误。但是在Java示例中找到的终点为https:// transcribestreaming。''。amazonaws.com,因此对此URL进行请求不会产生任何错误或响应。我正在从us-east-1地区尝试。

这是我正在尝试的代码。

const http2 = require('http2');
var aws4  = require('aws4');

var opts = {
  service: 'transcribe', 
  region: 'us-east-1', 
  path: '/stream-transcription', 
  headers:{
   'content-type': 'application/json',
   'x-amz-target': 'com.amazonaws.transcribe.Transcribe.StartStreamTranscription'
  }
}

var urlObj = aws4.sign(opts, {accessKeyId: '<access key>', secretAccessKey: '<aws secret>'});
const client = http2.connect('https://transcribestreaming.<region>.amazonaws.com');
client.on('error', function(err){
  console.error("error in request ",err);
});

const req = client.request({
  ':method': 'POST',
  ':path': '/stream-transcription',
  'authorization': urlObj.headers.Authorization,  
  'content-type': 'application/json',
  'x-amz-content-sha256': 'STREAMING-AWS4-HMAC-SHA256-EVENTS',
  'x-amz-target': 'com.amazonaws.transcribe.Transcribe.StartStreamTranscription',
  'x-amz-date': urlObj['headers']['X-Amz-Date'],
  'x-amz-transcribe-language-code': 'en-US',
  'x-amz-transcribe-media-encoding': 'pcm',
  'x-amz-transcribe-sample-rate': 44100
});

req.on('response', (headers, flags) => {
  for (const name in headers) {
    console.log(`${name}: ${headers[name]}`);
  }
});
let data = '';
req.on('data', (chunk) => { data += chunk; });
req.on('end', () => {
  console.log(`\n${data}`);
  client.close();
});
req.end();
Run Code Online (Sandbox Code Playgroud)

谁能指出我在这里所缺少的。我也找不到任何使用HTTP / 2实现此功能的示例。

更新:将Content-type更改为application / json时返回了响应状态200,但有以下异常:

`{"Output":{"__type":"com.amazon.coral.service#SerializationException"},"Version":"1.0"}`
Run Code Online (Sandbox Code Playgroud)

更新(2019年4月22日):

req.setEncoding('utf8');
req.write(audioBlob);

var audioBlob = new Buffer(JSON.stringify({
    "AudioStream": { 
       "AudioEvent": { 
          "AudioChunk": audioBufferData
     }
 }
Run Code Online (Sandbox Code Playgroud)

在结束请求之前,我要通过序列化添加“ audioblod”作为有效载荷。我的“ audioBufferData”是来自浏览器的原始PCM音频格式。我从文档有效负载中看到必须将其编码为“事件流编码”,但无法弄清楚如何实现它。

因此,在没有此事件流编码的情况下,我现在收到以下具有200个响应状态的异常。

{"Output":{"__type":"com.amazon.coral.service#UnknownOperationException"},"Version":"1.0"}
Run Code Online (Sandbox Code Playgroud)

Cal*_*vin 6

我联系了 AWS 支持,他们似乎无法获得与 NodeJS 一起使用的 HTTP/2 实现。

然而,他们现在提供了一种通过 websockets 直接与 Transcribe 流 API 交互的方法(博客文章在这里

如果这适合您的用例,我强烈建议您查看新的示例存储库:https : //github.com/aws-samples/amazon-transcribe-websocket-static

如果您在面向公众的页面中使用它,我建议您使用未经身份验证的 Cognito 会话来处理凭据检索。我已经在一个生产应用程序中使用了这个,所以请随时在任何其他问题中标记我。