Apricot
Apricot

Reputation: 3021

AWS Lambda : read image from S3 upload event

I am using Lambda to read image files when they are uploaded to S3 through a S3 trigger. The following is my code:

import json
import numpy as np
import face_recognition as fr

def lambda_handler(event, context):
    for record in event['Records']:
        bucket=record['s3']['bucket']['name']
        key = record['s3']['object']['key']
        print(bucket,key)

This correctly prints the bucket name and key. However how do I read the image so that I can run face-recognition module on the image. Can i generate the arn for each uploaded image and use it to read the same?

Upvotes: 0

Views: 1091

Answers (1)

Anon Coward
Anon Coward

Reputation: 10832

You can read the image from S3 directly:

s3 = boto3.client('s3')
resp = s3.get_object(Bucket=bucket, Key=key)
image_bytes = resp['Body'].read()

Upvotes: 2

Related Questions