diff --git a/backend/app/usecases/bot.py b/backend/app/usecases/bot.py index 1fa985259..3b17a4ebd 100644 --- a/backend/app/usecases/bot.py +++ b/backend/app/usecases/bot.py @@ -96,7 +96,10 @@ def _update_s3_documents_by_diff( for filename in deleted_filenames: document_path = compose_upload_document_s3_path(user_id, bot_id, filename) - delete_file_from_s3(DOCUMENT_BUCKET, document_path) + + # Ignore errors when deleting a non-existent file from the S3 bucket used in knowledge bases. + # This allows users to update bot if the uploaded file is missing after the bot is created. + delete_file_from_s3(DOCUMENT_BUCKET, document_path, ignore_not_exist=True) def create_new_bot(user_id: str, bot_input: BotInput) -> BotOutput: @@ -815,8 +818,12 @@ def issue_presigned_url( def remove_uploaded_file(user_id: str, bot_id: str, filename: str): + # Ignore errors when deleting a non-existent file from the S3 bucket used in knowledge bases. + # This allows users to update bot if the uploaded file is missing after the bot is created. delete_file_from_s3( - DOCUMENT_BUCKET, compose_upload_temp_s3_path(user_id, bot_id, filename) + DOCUMENT_BUCKET, + compose_upload_temp_s3_path(user_id, bot_id, filename), + ignore_not_exist=True, ) return diff --git a/backend/app/utils.py b/backend/app/utils.py index 265985832..d777f6409 100644 --- a/backend/app/utils.py +++ b/backend/app/utils.py @@ -97,17 +97,18 @@ def compose_upload_document_s3_path(user_id: str, bot_id: str, filename: str) -> return f"{user_id}/{bot_id}/documents/{filename}" -def delete_file_from_s3(bucket: str, key: str): +def delete_file_from_s3(bucket: str, key: str, ignore_not_exist: bool = False): client = boto3.client("s3", region_name=BEDROCK_REGION) # Check if the file exists - try: - client.head_object(Bucket=bucket, Key=key) - except ClientError as e: - if e.response["Error"]["Code"] == "404": - raise FileNotFoundError(f"The file does not exist in bucket.") - else: - raise + if not ignore_not_exist: + try: + client.head_object(Bucket=bucket, Key=key) + except ClientError as e: + if e.response["Error"]["Code"] == "404": + raise FileNotFoundError(f"The file does not exist in bucket.") + else: + raise response = client.delete_object(Bucket=bucket, Key=key) return response