File Upload (Firestore) Functions
Documentation for Firebase File Upload and URL Management Functions
Overview
These functions facilitate the uploading of files to Firebase Storage and managing URLs in Firestore for a supplier’s supporting documents. They include mechanisms for retrying uploads in case of failures and ensuring atomic updates in Firestore.
Function Descriptions
-
upload_file_to_firebase(user_id, file_path)- Uploads a file to Firebase Storage and makes it publicly accessible.
- Parameters:
user_id: The identifier for the supplier whose documents are being uploaded.file_path: The local path to the file that needs to be uploaded.
- Returns:
- The public URL of the uploaded file if successful.
- Errors:
- Prints and re-raises exceptions related to upload failures. Utilizes exponential backoff for retrying the upload up to five times.
-
add_url_to_generated_proposals(user_id, download_url)- Adds a document’s URL to a list of generated proposals in a Firestore document.
- Parameters:
user_id: The identifier for the supplier whose document URLs are being managed.download_url: The URL to add to the Firestore document.
- Functionality:
- Ensures atomic updates using Firestore transactions.
- Checks if the document already exists and updates it. If it does not exist, it prints a message (and can optionally create a new document).
- Errors:
- Prints and handles exceptions related to Firestore updates.
Usage Example
# To upload a file to Firebase and get the public URLuser_id = 'user123'file_path = '/path/to/file.docx'try: public_url = upload_file_to_firebase(user_id, file_path) print(f'Uploaded file is accessible at: {public_url}')except Exception as e: print(f'Error during file upload: {e}')
# To add the uploaded file's URL to the supplier's generated proposals in Firestoretry: add_url_to_generated_proposals(user_id, public_url) print('Document URL added to Firestore successfully.')except Exception as e: print(f'Error updating Firestore with new document URL: {e}')Key Features
- Resilience: The upload function uses exponential backoff to handle intermittent failures gracefully, ensuring that transient issues do not immediately cause the upload process to fail.
- Atomicity: The Firestore update is performed within a transaction, which ensures that the document update is atomic. This is crucial in concurrent environments where the same document might be updated by multiple processes.
These functions are critical for managing documents associated with supplier profiles in applications that require handling of sensitive or crucial files securely and reliably.