API网关二进制支持。wav文件

时间:2022-11-28 19:45:27

Currently I have a setup where an API Gateway accepts a .wav file from a POST request and sends the base64 encoded data from the audio file to a lambda function that I have as the handler using their Binary Support system.

目前我有一个设置,API网关接受POST请求中的.wav文件,并将音频文件中的base64编码数据发送给lambda函数,我使用它们的二进制支持系统作为处理程序。

I need to convert that base64 encoding of the audio file from the gateway back into a .wav format without writing to a new file and reading because AWS lambdas are a read-only file system.

我需要将音频文件的base64编码从网关转换回.wav格式,而不必写入新文件并读取,因为AWS lambdas是一个只读文件系统。

I've tried doing new Buffer(data, 'base64').toString('binary') hoping that would work but it didn't and there isn't a whole lot of information about how to do this.

我尝试过做新的Buffer(data, 'base64'). tostring ('binary'),希望它能起作用,但没有,而且关于如何做这个的信息也不多。

3 个解决方案

#1


3  

There are mainly two limitations you need to look into when using API Gateway and Lambda for your use case.

在使用API网关和Lambda用于您的用例时,主要需要考虑两个限制。

  • Maximum size of the audio file is 10MB
  • 音频文件的最大大小是10MB
  • If you need to store the file for processing in /tmp you can hold upto 512MB
  • 如果需要将文件存储到/tmp中进行处理,可以保存到512MB

Therefore mostly you will be constrained with 10MB limit for the audio file.

因此,大多数情况下,音频文件将受到10MB的限制。

For this kind of processing, one alternative option is to use the following flow.

对于这种处理,另一种选择是使用以下流。

  1. Using API Gateway and Lambda, request for a AWS CloudFront Signed URL to upload the audio file to AWS S3.
  2. 使用API网关和Lambda请求AWS CloudFront签名的URL将音频文件上载到AWS S3。
  3. Then upload the file directly from the client to S3.
  4. 然后将文件直接从客户端上载到S3。
  5. Having a Lambda trigger from S3 bucket to a Lambda function for processing (Max file size is 512 MB)
  6. 有一个Lambda从S3 bucket到Lambda函数进行处理(最大文件大小为512mb)

#2


0  

As long as your file is less than 512MB, you can write files to /tmp in a Lambda.

只要您的文件小于512MB,就可以用Lambda将文件写入/tmp。

#3


0  

Figure out the issue for anyone curious.

为任何好奇的人找出这个问题。

When API Gateway encodes the body of the request with their Binary Support system, they also encode the WebKitBoundary heading and tail data from the body if it exists.

当API网关通过二进制支持系统对请求的主体进行编码时,如果它存在,它们也会对WebKitBoundary标题和尾部数据进行编码。

It just cut out the front and tail of the encoding pertaining to that data so all that remained was strictly .wav data then recreated it as a Buffer using...

它只是剪掉了与数据相关的编码的前后,所以剩下的都是严格的.wav数据,然后使用…

let audio = new Buffer(body, 'base64').toString('binary')

// Sequence of replacements and slices to remove WebKitBoundary header and tail

const audioBuffer = new Buffer(audio, 'binary')

The result of audioBuffer is the exact equivalent of simply reading a .wav file using the fs module.

audioBuffer的结果完全等同于使用fs模块读取.wav文件。

#1


3  

There are mainly two limitations you need to look into when using API Gateway and Lambda for your use case.

在使用API网关和Lambda用于您的用例时,主要需要考虑两个限制。

  • Maximum size of the audio file is 10MB
  • 音频文件的最大大小是10MB
  • If you need to store the file for processing in /tmp you can hold upto 512MB
  • 如果需要将文件存储到/tmp中进行处理,可以保存到512MB

Therefore mostly you will be constrained with 10MB limit for the audio file.

因此,大多数情况下,音频文件将受到10MB的限制。

For this kind of processing, one alternative option is to use the following flow.

对于这种处理,另一种选择是使用以下流。

  1. Using API Gateway and Lambda, request for a AWS CloudFront Signed URL to upload the audio file to AWS S3.
  2. 使用API网关和Lambda请求AWS CloudFront签名的URL将音频文件上载到AWS S3。
  3. Then upload the file directly from the client to S3.
  4. 然后将文件直接从客户端上载到S3。
  5. Having a Lambda trigger from S3 bucket to a Lambda function for processing (Max file size is 512 MB)
  6. 有一个Lambda从S3 bucket到Lambda函数进行处理(最大文件大小为512mb)

#2


0  

As long as your file is less than 512MB, you can write files to /tmp in a Lambda.

只要您的文件小于512MB,就可以用Lambda将文件写入/tmp。

#3


0  

Figure out the issue for anyone curious.

为任何好奇的人找出这个问题。

When API Gateway encodes the body of the request with their Binary Support system, they also encode the WebKitBoundary heading and tail data from the body if it exists.

当API网关通过二进制支持系统对请求的主体进行编码时,如果它存在,它们也会对WebKitBoundary标题和尾部数据进行编码。

It just cut out the front and tail of the encoding pertaining to that data so all that remained was strictly .wav data then recreated it as a Buffer using...

它只是剪掉了与数据相关的编码的前后,所以剩下的都是严格的.wav数据,然后使用…

let audio = new Buffer(body, 'base64').toString('binary')

// Sequence of replacements and slices to remove WebKitBoundary header and tail

const audioBuffer = new Buffer(audio, 'binary')

The result of audioBuffer is the exact equivalent of simply reading a .wav file using the fs module.

audioBuffer的结果完全等同于使用fs模块读取.wav文件。