无需SDK的Amazon Transcribe流API [英] Amazon Transcribe Streaming API without SDK

查看:127
本文介绍了无需SDK的Amazon Transcribe流API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Go 1.11中的亚马逊新的流式转录API.目前,亚马逊仅提供Java SDK,因此我正在尝试低级方式.

I am trying to use Amazon's new streaming transcribe API from Go 1.11. Currently Amazon provides Java SDK only so I am trying the low-level way.

唯一相关的文档是此处,但是它不显示端点.我已经在

The only relevant piece of documentation is here but it does not show the endpoint. I have found it in a Java example that it is https://transcribestreaming.<region>.amazonaws.com and I am trying the Ireland region i.e. https://transcribestreaming.eu-west-1.amazonaws.com. Here is my code to open an HTTP/2 bi-directional stream:

import (
    "crypto/tls"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/aws/external"
    "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
    "golang.org/x/net/http2"
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "time"
)

const (
    HeaderKeyLanguageCode   = "x-amzn-transcribe-language-code"  // en-US
    HeaderKeyMediaEncoding  = "x-amzn-transcribe-media-encoding" // pcm only
    HeaderKeySampleRate     = "x-amzn-transcribe-sample-rate"    // 8000, 16000 ... 48000
    HeaderKeySessionId      = "x-amzn-transcribe-session-id"     // For retrying a session. Pattern: [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}
    HeaderKeyVocabularyName = "x-amzn-transcribe-vocabulary-name"
    HeaderKeyRequestId = "x-amzn-request-id"
)

...

region := "eu-west-1"

cfg, err := external.LoadDefaultAWSConfig(aws.Config{
    Region: region,
})
if err != nil {
    log.Printf("could not load default AWS config: %v", err)
    return
}

signer := v4.NewSigner(cfg.Credentials)

transport := &http2.Transport{
    TLSClientConfig: &tls.Config{
        // allow insecure just for debugging
        InsecureSkipVerify: true,
    },
}
client := &http.Client{
    Transport: transport,
}

signTime := time.Now()

header := http.Header{}
header.Set(HeaderKeyLanguageCode, "en-US")
header.Set(HeaderKeyMediaEncoding, "pcm")
header.Set(HeaderKeySampleRate, "16000")
header.Set("Content-type", "application/json")

// Bi-directional streaming via a pipe.
pr, pw := io.Pipe()

req, err := http.NewRequest(http.MethodPost, "https://transcribestreaming.eu-west-1.amazonaws.com/stream-transcription", ioutil.NopCloser(pr))
if err != nil {
    log.Printf("err: %+v", err)
    return
}
req.Header = header

_, err = signer.Sign(req, nil, "transcribe", region, signTime)
if err != nil {
    log.Printf("problem signing headers: %+v", err)
    return
}

// This freezes and ends after 5 minutes with "unexpected EOF".
res, err := client.Do(req)
...

问题是执行请求(client.Do(req))会冻结五分钟,然后以意外的EOF"错误结束.

Problem is that executing the request (client.Do(req)) freezes for five minutes and then ends with the "unexpected EOF" error.

有什么想法我做错了吗?有人在没有Java SDK的情况下成功使用了新的流式转录API吗?

Any ideas what I am doing wrong? Did someone successfully use the new streaming transcribe API without the Java SDK?

编辑(2019年3月11日):

我再次对此进行了测试,现在它没有超时,但立即返回200 OK响应.响应正文中有一个例外":{"Output":{"__type":"com.amazon.coral.service#SerializationException"},"Version":"1.0"}

I tested this again and now it does not time out but immediately returns 200 OK response. There is an "exception" in the response body though: {"Output":{"__type":"com.amazon.coral.service#SerializationException"},"Version":"1.0"}

我尝试使用io.Pipe(如上面的代码)以及文档中描述的JSON正文打开HTTP2流:

I tried opening the HTTP2 stream with io.Pipe (like the code above) and also with a JSON body described in the documentation:

{
    "AudioStream": { 
        "AudioEvent": { 
            "AudioChunk": ""
        }
    }
}

结果是相同的.

编辑(2019年3月13日):

如@gpeng所述,从标题中删除content-type将修复SerializationException.但是然后有一个IAM异常,需要将transcription:StartStreamTranscription权限添加到您的IAM用户.虽然在AWS IAM控制台中没有任何地方,但必须作为自定义JSON权限手动添加:/

As mentioned by @gpeng, removing the content-type from headers will fix the SerializationException. But then there is an IAM exception and it is needed to add the transcription:StartStreamTranscription permission to your IAM user. That is though nowhere in the AWS IAM console and must be added manually as a custom JSON permission :/

此处,其中显示了错误的host和新的content-type(请勿使用该content-type,请求将返回404).

There is also a new/another documentation document here which shows incorrect host and a new content-type (do not use that content-type, the request will return 404 with it).

删除content-type,并添加新的权限后,现在我得到了异常{"Message":"A complete signal was sent without the preceding empty frame."}.还要永久写入管道块,所以我又被卡住了.新文档中描述的消息与旧文档中的消息有所不同,现在是最终的二进制文件,但是我不理解它们.有什么想法如何在Go中发送这样的HTTP2消息吗?

After removing the content-type, and adding the new permission, now I am getting an exception {"Message":"A complete signal was sent without the preceding empty frame."}. Also writing to the pipe blocks forever, so I am stuck again. The messages described in the new documentation are different than in the old one, now finally binary, but I do not understand them. Any ideas how to send such HTTP2 messages in Go?

编辑(2019年15月15日):*

如果收到有关签名不匹配的HTTP 403错误,请不要设置transfer-encodingx-amz-content-sha256 HTTP标头.设置它们后,请使用AWS开发工具包的V4签名者对请求进行签名,然后收到HTTP 403 The request signature we calculated does not match the signature you provided.

If you get HTTP 403 error about signature mismatch, then do not set the transfer-encoding and x-amz-content-sha256 HTTP headers. When I set them, sign the request with AWS SDK's V4 signer, then I receive HTTP 403 The request signature we calculated does not match the signature you provided.

推荐答案

我寻求AWS支持,他们现在建议尽可能使用websocket而不是HTTP/2(博客文章

I reached out to AWS support and they now recommend using websockets instead of HTTP/2 when possible (blog post here)

如果适合您的用例,我强烈建议您在以下位置查看新的示例存储库:

If this fits your usecase I would highly recommend checking out the new example repo at: https://github.com/aws-samples/amazon-transcribe-websocket-static which shows a browser-based solution in JS.

我还注意到,演示的作者在其个人Github上有一个明确的示例,网址为:

I've also noticed that the author of the demo has an express example on his personal Github at: https://github.com/brandonmwest/amazon-transcribe-websocket-express but I haven't confirmed if this is working.

赞赏这些示例不是在Python中实现的,但我认为使用Websocket客户端而不是HTTP/2会更好(幸运的是,这仍然有点可怕:P)

Appreciate these examples aren't in Python but I think you'll have better luck using the Websocket client as opposed to HTTP/2 (which let's be honest, is still a bit terrifying :P)

这篇关于无需SDK的Amazon Transcribe流API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆