简体   繁体   English

返回音频文件作为来自 AWS 的响应 Lambda function

[英]Returning an audio file as response from AWS Lambda function

I have an AWS API Gateway calling a Lambda function (with Proxy integration), which should return an audio file.我有一个 AWS API 网关调用 Lambda function(代理集成),它应该返回一个音频文件。

Let's say that I have my audio as 'bytes' object. How do I go about returning it, such that it can immediately be recognized and played by the browser (eg Firefox)?假设我的音频为“字节”object。如何返回它 go,以便它可以立即被浏览器(例如 Firefox)识别和播放?

Here's what I got, but, clearly, that doesn't really work.这就是我得到的,但是,显然,这并没有真正起作用。

sound: bytes

return {
    'statusCode': 200,
    'headers': {
        'Content-Type': 'audio/mpeg'
    },
    'isBase64Encoded': True,
    'body': base64.b64encode(sound)
}

Solution for my case ( proxy integration )我的案例的解决方案(代理集成

  1. Base-64 encode the bytes object (eg base64.b64encode(sound) in Python) and put the result in 'body' of your response dictionary. Base-64 编码字节 object(例如 Python 中的base64.b64encode(sound) )并将结果放入响应字典的'body'中。
  2. Set 'isBase64Encoded' to true in your response dictionary.在您的响应字典'isBase64Encoded'设置为true
  3. In API settings, set Binary Media Types to the appropriate media type and don't forget to set the client's Accept Header to the same value.在 API 设置中,将Binary Media Types设置为适当的媒体类型,并且不要忘记将客户端的 Accept Header 设置为相同的值。

Resources that helped me directly直接帮助我的资源

Resources that might be useful可能有用的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM