Skip to content

Commit

Permalink
feat: Ignore file not exists error when delete files in KB (#663)
Browse files Browse the repository at this point in the history
* feat: Ignore file not exists error when delete files in KB

* chore: add comment
  • Loading branch information
konokenj authored Jan 16, 2025
1 parent 69b47d7 commit 8afe1c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 9 additions & 2 deletions backend/app/usecases/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions backend/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8afe1c9

Please sign in to comment.