Skip to content

Instantly share code, notes, and snippets.

@aconz2
Last active December 18, 2019 21:25
Show Gist options
  • Save aconz2/1d86d0cfe3bfd9c9112e22c54dcf1c03 to your computer and use it in GitHub Desktop.
Save aconz2/1d86d0cfe3bfd9c9112e22c54dcf1c03 to your computer and use it in GitHub Desktop.
monkey patch s3 streamingbody to be streamable for csv for example
import boto3
import io
import csv
from functools import partial
s3c = boto3.client('s3')
def logging_read(read, amt):
print('READ {}'.format(amt))
return read(amt)
def csv_from_object(bucket, key):
body = s3c.get_object(Bucket=bucket, Key=key)['Body']
# uncomment to see reads
# setattr(body, 'read', partial(logging_read, body.read))
# these "implement" the io.IOBase interface
setattr(body, 'readable', lambda: True)
setattr(body, 'writable', lambda: False)
setattr(body, 'seekable', lambda: False)
setattr(body, 'closed', False)
setattr(body, 'flush', lambda: 'noop')
with io.TextIOWrapper(body, newline='') as buf_wrapper:
# we have to yield from instead of return to keep TextIOWrapper alive
yield from csv.DictReader(buf_wrapper)
for row in csv_from_object('bucket', 'something.csv'):
print(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment