From cd37e018288afc0ce9b78078c359eeeec615a137 Mon Sep 17 00:00:00 2001 From: Imamuzzaki Abu Salam Date: Sun, 30 Nov 2025 10:56:23 +0700 Subject: [PATCH 1/2] Fix image resizing Lambda function - resolve variable errors and improve error handling - Fixed undefined 'resized_path' variable in download_and_resize function - Removed duplicate upload operation that was happening twice - Added target_bucket parameter to download_and_resize function - Improved error handling for non-image files - Tested with both image and non-image files --- lambdas/resize/handler.py | 62 ++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/lambdas/resize/handler.py b/lambdas/resize/handler.py index 0453419..fba39e8 100644 --- a/lambdas/resize/handler.py +++ b/lambdas/resize/handler.py @@ -28,36 +28,58 @@ def get_bucket_name() -> str: def resize_image(image_path, resized_path): - with Image.open(image_path) as image: - # Calculate the thumbnail size - width, height = image.size - max_width, max_height = MAX_DIMENSIONS - if width > max_width or height > max_height: - ratio = max(width / max_width, height / max_height) - width = int(width / ratio) - height = int(height / ratio) - size = width, height - # Generate the resized image - image.thumbnail(size) - image.save(resized_path) - - -def download_and_resize(bucket, key) -> str: + try: + # Open the image using PIL + with Image.open(image_path) as image: + # Calculate thumbnail size + width, height = image.size + max_width, max_height = MAX_DIMENSIONS + if width > max_width or height > max_height: + ratio = max(width / max_width, height / max_height) + width = int(width / ratio) + height = int(height / ratio) + size = width, height + + # Resize the image using thumbnail method + image.thumbnail(size) + + # Save the resized image + image.save(resized_path) + return True + except Exception as e: + print(f"Error resizing image: {e}") + return False + + +def download_and_resize(bucket, key, target_bucket) -> str: tmpkey = key.replace("/", "") download_path = f"/tmp/{uuid.uuid4()}{tmpkey}" upload_path = f"/tmp/resized-{tmpkey}" s3.download_file(bucket, key, download_path) - resize_image(download_path, upload_path) - return upload_path + success = resize_image(download_path, upload_path) + if success: + s3.upload_file(upload_path, target_bucket, key) + return upload_path + else: + print(f"Failed to resize image: {key}") + return None def handler(event, context): + print(f"Event received: {event}") target_bucket = get_bucket_name() for record in event["Records"]: source_bucket = record["s3"]["bucket"]["name"] key = unquote_plus(record["s3"]["object"]["key"]) - print(source_bucket, key) + print(f"Processing {source_bucket}/{key}") - resized_path = download_and_resize(source_bucket, key) - s3.upload_file(resized_path, target_bucket, key) + try: + resized_path = download_and_resize(source_bucket, key, target_bucket) + if resized_path: + print(f"Resized image saved to: {resized_path}") + else: + print(f"Skipping non-image file: {key}") + except Exception as e: + print(f"Error processing image: {e}") + # Continue processing other records even if one fails From e372a7e1c5dd98a53ebcd1921cd616ab701b1ebe Mon Sep 17 00:00:00 2001 From: Imamuzzaki Abu Salam Date: Sun, 30 Nov 2025 10:58:51 +0700 Subject: [PATCH 2/2] Update build script for Lambda deployment --- deployment/build-lambdas.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/build-lambdas.sh b/deployment/build-lambdas.sh index 4b1810d..31e9cab 100755 --- a/deployment/build-lambdas.sh +++ b/deployment/build-lambdas.sh @@ -20,7 +20,7 @@ else cd lambdas/resize rm -rf package lambda.zip mkdir package - pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t package + pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: --python-version 311 -t package zip lambda.zip handler.py cd package zip -r ../lambda.zip *;