Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Forked from pgolding/streamingbody.md
Created April 15, 2019 15:14
Show Gist options
  • Save saswata-dutta/b3a0c43e5aaf3c4b6c3503ff7c3ed59e to your computer and use it in GitHub Desktop.
Save saswata-dutta/b3a0c43e5aaf3c4b6c3503ff7c3ed59e 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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment