I am creating a Lambda function to receive a base64-encoded image in a POST request, and save it into a S3 bucket. I need to open the image in Pillow for some processing, which is irrelevant so I skipped some code. I received this error when invoking the Lambda function HTTPClientError: An HTTP Client raised an unhandled exception: sequence item 0: expected str instance, bytes found
. My Lambda code is as follows:
import logging
import boto3
import base64
from io import BytesIO
from botocore.exceptions import ClientError
from PIL import Image
def lambda_handler(event, context):
s3_bucket = 'xxx' # My S3 bucket_name
base64_image = event.get('body', '')
if not base64_image:
return {
'statusCode': 400,
'body': 'No image data found in the request body.'
}
image = Image.open(BytesIO(base64.b64decode(base64_image)))
# Skipped some imaging processing code ...
image_bytes = BytesIO()
image.save(image_bytes, format='PNG')
image_bytes.seek(0)
s3_client.upload_fileobj(image_bytes, s3_bucket, 'haha', ExtraArgs={'ContentType': 'image/png'})
Any help is appreciated!
2
Answers
Answering my own question in case someone encounter this. My issue is I have added a python
requests
layer which somehow messed with how Lambda send the object to s3 using https. The error is gone if I remove the layer.@SamTest I encountered the same error on my Lambda function.
When you say removing "requests" layer, was that because you installed it to Lambda layer before?
I checked mine, and I do not have this layer installed. Is this pre-built underlying Lambda? thanks!