[Python] Read function for botocore.response.StreamingBody silently failing in lambda function

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to the data of an object in S3 in my lambda function with the following code:

    session = boto3.Session()
    s3d = boto3.resource('s3')
    s3_client = session.client('s3')
    s3_resource = session.resource('s3')

    def lambda_handler(event, context):
    # Get the S3 bucket and object name from the event
    bucket = event['Records'][0]['s3']['bucket']['name']
    objectKey = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
    print(bucket)
    print(objectKey)

    content_object = s3_resource.Object(bucket, objectKey)
    file_content = content_object.get()['Body'].read(None).decode('utf-8')
    metadata = json.loads(file_content)


    I know from logging debug messages that this is failing on the read() function. I've tried putting it in a try/except block and printing the error, but that isn't working. I'm able to get content_object.get()['Body'] by itself and confirm it is a botocore.response.StreamingBody object, and so the .read function should return all of the data according to the docs. Instead, the lambda is failing silently.

    Continue reading...

Compartilhe esta Página