Skip to content

Commit e44c29e

Browse files
committed
fix: image upload in s3 bucket
1 parent 1175654 commit e44c29e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

routes/admin.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ class AddRequest(BaseModel):
163163
question_data: QuestionData
164164

165165
async def upload_to_s3(file: UploadFile, bucket_name: str) -> str:
166-
s3_client = boto3.client('s3')
166+
s3_client = boto3.client(
167+
's3',
168+
aws_access_key_id=os.getenv("MY_AWS_ACCESS_KEY"),
169+
aws_secret_access_key=os.getenv("MY_AWS_SECRET_KEY"),
170+
region_name=os.getenv("MY_AWS_REGION")
171+
)
167172

168173
file_extension = file.filename.split('.')[-1]
169174
unique_filename = f"{uuid.uuid4()}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.{file_extension}"
@@ -194,20 +199,26 @@ async def add_question(
194199
admin_result = await verify_admin(authorization, domain)
195200
if isinstance(admin_result, JSONResponse):
196201
return admin_result
202+
197203
quiz_table = resources['quiz_table']
198204

199205
question_data_dict = {"question": question}
200206

201207
if options:
202-
options=options[0]
203-
options=json.loads(options)
208+
options = options[0]
209+
options = json.loads(options)
204210
question_data_dict["options"] = options
205211

206212
if correctIndex:
207213
question_data_dict["correctIndex"] = int(correctIndex)
214+
208215
if image:
209-
image_url = await upload_to_s3(image, bucket_name=S3_BUCKET_NAME)
210-
question_data_dict["image_url"] = image_url
216+
try:
217+
image_url = await upload_to_s3(image, bucket_name=S3_BUCKET_NAME)
218+
question_data_dict["image_url"] = image_url
219+
except Exception as e:
220+
print(f"S3 upload error: {str(e)}")
221+
raise Exception(f"Failed to upload image: {str(e)}")
211222

212223
response = quiz_table.get_item(Key={'qid': domain})
213224
field = response.get('Item')
@@ -227,11 +238,12 @@ async def add_question(
227238
)
228239

229240
except Exception as e:
241+
# Enhanced error logging
242+
print(f"Error in add_question: {str(e)}")
230243
return JSONResponse(
231244
status_code=400,
232245
content={"detail": f"Error processing request: {str(e)}"}
233246
)
234-
235247

236248
class QualificationRequest(BaseModel):
237249
user_email: str

0 commit comments

Comments
 (0)