Skip to content
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

feat: Ignore file not exists error when delete files in KB #663

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions backend/app/usecases/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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)
delete_file_from_s3(DOCUMENT_BUCKET, document_path, ignore_not_exist=True)
konokenj marked this conversation as resolved.
Show resolved Hide resolved


def create_new_bot(user_id: str, bot_input: BotInput) -> BotOutput:
Expand Down Expand Up @@ -805,7 +805,9 @@ def issue_presigned_url(

def remove_uploaded_file(user_id: str, bot_id: str, filename: str):
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,
konokenj marked this conversation as resolved.
Show resolved Hide resolved
)
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 @@ -92,17 +92,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
Loading