from fastapi import FastAPI, File, UploadFile import tempfile import os # Assuming Celery is already set up and imported correctly in the project. from celery import shared_task # Import your Celery instance here. app = FastAPI() @app.post("/uploadfile/") async def upload_file(file: UploadFile = File(...)): contents = await file.read() # Save the file to a temporary directory temp_dir = tempfile.gettempdir() temp_file_path = os.path.join(temp_dir, file.filename) with open(temp_file_path, 'wb') as f: f.write(contents) # Call a Celery task that deletes the file after processing. delete_temp_file.delay(temp_file_path) # Assuming this is your Celery task name. return {"filename": file.filename} # Define the Celery task to delete the temporary file @shared_task def delete_temp_file(file_path): os.remove(file_path)