Event Stream Reference#

botocore.eventstream#

class botocore.eventstream.EventStream(raw_stream, output_shape, parser, operation_name)#

Wrapper class for an event stream body.

This wraps the underlying streaming body, parsing it for individual events and yielding them as they come available through the iterator interface.

The following example uses the S3 select API to get structured data out of an object stored in S3 using an event stream.

Example:

from botocore.session import Session

s3 = Session().create_client('s3')
response = s3.select_object_content(
    Bucket='bucketname',
    Key='keyname',
    ExpressionType='SQL',
    RequestProgress={'Enabled': True},
    Expression="SELECT * FROM S3Object s",
    InputSerialization={'CSV': {}},
    OutputSerialization={'CSV': {}},
)
# This is the event stream in the response
event_stream = response['Payload']
end_event_received = False
with open('output', 'wb') as f:
    # Iterate over events in the event stream as they come
    for event in event_stream:
        # If we received a records event, write the data to a file
        if 'Records' in event:
            data = event['Records']['Payload']
            f.write(data)
        # If we received a progress event, print the details
        elif 'Progress' in event:
            print(event['Progress']['Details'])
        # End event indicates that the request finished successfully
        elif 'End' in event:
            print('Result is complete')
            end_event_received = True
if not end_event_received:
    raise Exception("End event not received, request incomplete.")
close()#

Closes the underlying streaming body.