-
Notifications
You must be signed in to change notification settings - Fork 146
fix: handle NoSuchKey when image_features.pkl is missing in S3 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |||||||||||||||||||
| import json | ||||||||||||||||||||
| from models import Product, Category, User, db, Comment, product_categories | ||||||||||||||||||||
| import boto3 | ||||||||||||||||||||
| from botocore.exceptions import NoCredentialsError | ||||||||||||||||||||
| from botocore.exceptions import NoCredentialsError, ClientError | ||||||||||||||||||||
| from vulnerable_image_processor import process_image | ||||||||||||||||||||
| from sklearn.metrics.pairwise import cosine_similarity | ||||||||||||||||||||
| import pickle | ||||||||||||||||||||
|
|
@@ -236,8 +236,23 @@ def product_lookup(): | |||||||||||||||||||
|
|
||||||||||||||||||||
| features_file_key = 'image_features.pkl' # S3 key for the features file | ||||||||||||||||||||
|
|
||||||||||||||||||||
| response = s3.get_object(Bucket=bucket_name, Key=features_file_key) | ||||||||||||||||||||
| features = pickle.loads(response['Body'].read()) | ||||||||||||||||||||
| try: | ||||||||||||||||||||
| response = s3.get_object(Bucket=bucket_name, Key=features_file_key) | ||||||||||||||||||||
| features = pickle.loads(response['Body'].read()) | ||||||||||||||||||||
| except ClientError as e: | ||||||||||||||||||||
| if e.response['Error']['Code'] == 'NoSuchKey': | ||||||||||||||||||||
| app.logger.error( | ||||||||||||||||||||
| 'image_features.pkl not found in S3 bucket %s. ' | ||||||||||||||||||||
| 'Please run the image preprocessing pipeline in the SageMaker notebook first.', | ||||||||||||||||||||
| bucket_name | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| return jsonify({ | ||||||||||||||||||||
| 'error': ( | ||||||||||||||||||||
| 'Image features file not found. Please run the image preprocessing ' | ||||||||||||||||||||
| 'pipeline in the SageMaker notebook first. See README for setup instructions.' | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| }), 503 | ||||||||||||||||||||
| raise | ||||||||||||||||||||
|
||||||||||||||||||||
| raise | |
| app.logger.exception( | |
| "Error retrieving features file '%s' from S3 bucket '%s'", | |
| features_file_key, | |
| bucket_name, | |
| ) | |
| return jsonify({ | |
| 'error': 'Unable to retrieve image features from storage. Please try again later.' | |
| }), 500 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log line drops the actual S3 error details (code/message/request-id), which can make debugging harder. Consider including
e.response['Error']fields (or logging withexc_info=True) while still returning a user-friendly response to the client.