简体   繁体   中英

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.

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)?

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.
  2. Set 'isBase64Encoded' to true in your response dictionary.
  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.

Resources that helped me directly

Resources that might be useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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