diff --git a/middleware/app/api/routes/upload.py b/middleware/app/api/routes/upload.py index 90008ea..b8a158f 100644 --- a/middleware/app/api/routes/upload.py +++ b/middleware/app/api/routes/upload.py @@ -356,6 +356,7 @@ def delete_upload_return_done( keys = {"upload_id": upload_id} dynamodb.delete_item(keys) + storage.delete_folder(upload_id) log.info("Exiting {}".format(FUNCTION_NAME)) return {"status": "Done"} diff --git a/middleware/app/storage/cloudflare_r2.py b/middleware/app/storage/cloudflare_r2.py index c8719b7..86385d6 100644 --- a/middleware/app/storage/cloudflare_r2.py +++ b/middleware/app/storage/cloudflare_r2.py @@ -133,5 +133,25 @@ def get_file_info(self, file_path: str): ) raise HTTPException(status_code=500, detail=str(e)) + def delete_folder(self, folder_name: str): + FUNCTION_NAME = "delete_folder()" + log.info("Entering {}".format(FUNCTION_NAME)) + + try: + response = self.r2.list_objects_v2(Bucket=self.bucket_name, Prefix=folder_name) + if "Contents" in response: + for obj in response["Contents"]: + self.r2.delete_object(Bucket=self.bucket_name, Key=obj["Key"]) + log.info("Deleted UploadID: {}".format(folder_name)) + except Exception as e: + log.error( + "EXCEPTION occurred deleting folder in R2.\nERROR: {}".format( + str(e) + ) + ) + raise HTTPException(status_code=500, detail=str(e)) + + log.info("Exiting {}".format(FUNCTION_NAME)) + def _get_exact_format(self, file_format): return file_format.split("/")[-1] diff --git a/middleware/app/storage/storage.py b/middleware/app/storage/storage.py index 16067b1..e1ccc47 100644 --- a/middleware/app/storage/storage.py +++ b/middleware/app/storage/storage.py @@ -25,3 +25,7 @@ def is_file_present(self, file_path): @abstractmethod def get_file_info(self, file_path: str): pass + + @abstractmethod + def delete_folder(self, folder_name: str): + pass