I am trying to read a file from the S3 bucket, get the file in an io.BufferedReader and return it in via lambda function that would later decode to JSON. I am getting an error message as
Unable to marshal response: Object of type BufferedReader is not JSON serializable
My code is mentioned below.
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = "bucketID"
body = []
for record in event['key']:
with open('/tmp/2021-10-11T06:23:29:691472.pdf', 'wb') as f:
s3.download_fileobj(bucket, "basedir/file.pdf", f)
f = open("/tmp/2021-10-11T06:23:29:691472.pdf", "rb")
body.append(f)
return {
"statusCode": 200,
"file":f,
"content":f.read()
}
Error Response from lambda
Response
{
"errorMessage": "Unable to marshal response: Object of type BufferedReader is not JSON serializable",
"errorType": "Runtime.MarshalError",
"requestId": "10aea120-kyc-jpk-bnce-7123eTyda64",
"stackTrace": []
}
I am using the AWS-Lambda Python function