Skip to content

Instantly share code, notes, and snippets.

@pgolding
Last active January 30, 2024 16:28
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pgolding/9083a6f3590067e3ffe694c947ef90a3 to your computer and use it in GitHub Desktop.
Save pgolding/9083a6f3590067e3ffe694c947ef90a3 to your computer and use it in GitHub Desktop.
Handling of StreamingBody Response from Invoking a Lambda Function

Handling of StreamingBody Response from Invoking a Lambda Function

When calling a Lambda Function via Boto3, the returned object is something like:

{u'Payload': <botocore.response.StreamingBody object at 0x7f8d5b62ac50>, 
'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '5bdbb3ca-3b35-11e7-9816-397b140c3bac', 'HTTPHeaders': {'x-amzn-requestid': '5bdbb3ca-3b35-11e7-9816-397b140c3bac', 'content-length': '1636', 'x-amzn-trace-id': 'root=1-591ca199-00d34d5474d16275ec2c8d10;sampled=0', 'x-amzn-remapped-content-length': '0', 'connection': 'keep-alive', 'date': 'Wed, 17 May 2017 19:16:41 GMT', 'content-type': 'application/json'}}, u'StatusCode': 200}

The Payload parameter is <botocore.response.StreamingBody> which is a data streaming object.

import botocore.response as br
streaming_obj = br.StreamingBody(raw_stream=None,content_length=5000)
streaming_obj.read(amt=)

This is essentially a pointer to a data source that can be consumed as a stream via:

NUM_OF_BYTES = 1000
streaming.read(amt=NUM_OF_BYTES)

Or, if NUM_OF_BYTES == None then it will return the entire stream.

This data has to be made available to other functions, so one approach is to convert to string:

string_data = streaming_obj.read().decode("utf-8")

Returning to our Lambda Function, then the streaming object is referenced via a Payload parameter (see above) and typically we'd expect to see a JSON object (say):

res_json = json.loads(res['Payload'].read().decode("utf-8"))
@ranjit009
Copy link

I have the similar error but unable to resolve it, Please help!

@IanPhilips
Copy link

I'm getting: "errorMessage": "<botocore.response.StreamingBody object at 0x7f94597afba8> is not JSON serializable",

@IanPhilips
Copy link

scratch that, I'm actually getting: "errorMessage": "'utf-8' codec can't decode byte 0xff in position 45: invalid start byte",

@wgodwin
Copy link

wgodwin commented Apr 10, 2018

I'm also getting the utf-8 error when trying to read an excel file into a string. Are there any things I can try to maybe get past this issue?

@sabotagebeats
Copy link

i also am unable to get text out of my StreamingBody object

@Chewyslaw
Copy link

This was what I was looking for, thanks!

@qkponton
Copy link

any updates?

@manycoding
Copy link

manycoding commented Nov 22, 2020

If python version > 3.5, simply json.loads(response["Payload"].read())

@arcones
Copy link

arcones commented Jan 30, 2024

thanks a ton!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment