简体   繁体   中英

How to return io.BufferedReader through AWS Lambda(python)?

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 = "document.as.a.service.test"
    body = []
    for record in event['uuid_filepath']:
        with open('/tmp/2021-10-11T06:23:29:691472.pdf', 'wb') as f:
            s3.download_fileobj(bucket, "123TfZ/2021-10-11T06:23:29:691472.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-fafc-4080-a598-79a89182da64",
  "stackTrace": []
}

I am using the AWS-Lambda Python function

f.read() return bytes and JSON does not support binary data. Also "file":f is incorrect. Guess it should be a filename. Anyway, usually you would return binary data in JSON as base64 :

import base64

s3 = boto3.client('s3')
def lambda_handler(event, context):
    bucket = "document.as.a.service.test"
    body = []
    for record in event['uuid_filepath']:
        with open('/tmp/2021-10-11T06:23:29:691472.pdf', 'wb') as f:
            s3.download_fileobj(bucket, "123TfZ/2021-10-11T06:23:29:691472.pdf", f)
        f = open("/tmp/2021-10-11T06:23:29:691472.pdf", "rb")
    body.append(f)
    
    return {
        "statusCode": 200,
        "file": "2021-10-11T06:23:29:691472.pdf",
        "content": base64.b64encode(f.read())
    }

Then on the client side, you have to decode base64 to binary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM