def lambda_handler()

in functions/analyse_image/app.py [0:0]


def lambda_handler(event, context):
    print("Analysing images")
    print(event)
    for msg in event["Records"]:
        msg_payload = json.loads(msg["body"])
        if "Records" in msg_payload:
            bucket = msg_payload["Records"][0]["s3"]["bucket"]["name"]
            image = msg_payload["Records"][0]["s3"]["object"]["key"].replace("+", " ")

            image_file_response = s3_client.get_object(Bucket=bucket, Key=image)
            response = lookoutforvision_client.detect_anomalies(
                ProjectName=projectname,
                ModelVersion=projectmodelversion,
                Body=image_file_response["Body"].read(),
                ContentType=image_file_response["ContentType"],
            )

            # Get the anomaly detection result
            detect_anomaly_result = response["DetectAnomalyResult"]
            # write image to final bucket and delete from incoming bucket
            finalbucket = os.environ["Final_S3_Bucket_Name"]
            copy_source = {"Bucket": bucket, "Key": image}
            random_letters = "".join(
                random.choice(string.ascii_letters) for i in range(10)
            )
            put_image_name = random_letters + "-" + image
            s3.meta.client.copy(copy_source, finalbucket, put_image_name)

            # Dump json file with label data in final bucket
            json_object = json.dumps(detect_anomaly_result)
            s3_client.put_object(
                Body=str(json_object), Bucket=finalbucket, Key=put_image_name + ".json"
            )

            # Delete file from incoming s3
            s3_client.delete_object(
                Bucket=bucket,
                Key=image,
            )

        else:
            # Invalid Message - To Be removed from Queue
            print("Invalid msg: ", msg)

    return {"status": "200"}