This Python backend provides secure document upload functionality to Google Drive for the TradeFlow Secure Trade Documentation Portal.
- Flask API with CORS support
- Google Drive API integration with service account authentication
- Dynamic folder creation per user/company
- File validation (MIME types, size limits)
- Secure file naming with timestamps
- Comprehensive logging
- Go to Google Cloud Console
- Create a new project (e.g., "TradeFlow Document Portal")
- Enable billing for the project
- In the Google Cloud Console, go to APIs & Services > Library
- Search for "Google Drive API"
- Click Enable
- Go to APIs & Services > Credentials
- Click Create Credentials > Service Account
- Enter a name (e.g., "tradeflow-upload-service")
- Click Create and Continue
- Grant role: Editor (or create a custom role with Drive permissions)
- Click Done
- Click on the service account you just created
- Go to the Keys tab
- Click Add Key > Create New Key
- Select JSON format
- Click Create
- The JSON key file will download automatically - keep this secure!
- Go to Google Drive
- Create a new folder named "GlobalTradeDocuments"
- Right-click the folder > Share
- Add the service account email (found in the JSON key file under
client_email) - Set permission to Editor
- Copy the folder ID from the URL (the string after
/folders/)
- Rename the downloaded JSON key file to
service-account.json - Place it in the
backend/directory - Set environment variables (or edit
config.py):
# Linux/Mac
export GOOGLE_SERVICE_ACCOUNT_FILE="/path/to/service-account.json"
export GOOGLE_DRIVE_MAIN_FOLDER_ID="your-folder-id-here"
# Windows
set GOOGLE_SERVICE_ACCOUNT_FILE=C:\path\to\service-account.json
set GOOGLE_DRIVE_MAIN_FOLDER_ID=your-folder-id-herecd backend
pip install -r requirements.txtpython upload_service.pyThe server will start on http://localhost:5000
export FLASK_DEBUG=False
export FLASK_HOST=0.0.0.0
export FLASK_PORT=5000
python upload_service.pyGET /health
POST /upload-document
Content-Type: multipart/form-data
Parameters:
- file: The file to upload (PDF, JPG, PNG, max 10MB)
- document_type: Type of document (e.g., 'commercial-invoice')
- document_name: Display name of document
- company_name: Company name (required)
- user_email: User email address (required)
- contact_name: Contact person name (optional)
Response:
{
"status": "success",
"message": "File uploaded successfully",
"filename": "CommercialInvoice_ABC_20250101_120000.pdf",
"file_id": "1a2b3c4d...",
"web_view_link": "https://drive.google.com/file/d/...",
"folder_id": "5e6f7g8h...",
"timestamp": "2025-01-01T12:00:00"
}
GET /list-documents?company_name=ABC&user_email=user@example.com
POST /delete-document
Content-Type: application/json
{
"file_id": "1a2b3c4d..."
}
GlobalTradeDocuments (Main Folder)
│
├── TradeDocs_ABCImports_john_at_email_com
│ ├── CommercialInvoice_ABCImports_20250101_120000.pdf
│ ├── PackingList_ABCImports_20250101_120005.pdf
│ └── BillOfLading_ABCImports_20250101_120010.pdf
│
├── TradeDocs_XYZExports_maria_at_email_com
│ ├── CertificateOfOrigin_XYZExports_20250101_130000.pdf
│ └── CustomsDeclaration_XYZExports_20250101_130005.pdf
- Never commit
service-account.jsonto version control - Use environment variables for sensitive configuration
- Restrict service account permissions to only necessary Drive operations
- Validate all file uploads (type, size, content)
- Use HTTPS in production
- Implement rate limiting for production use
- Log all upload activities for audit purposes
- Ensure
service-account.jsonis in thebackend/directory - Check the
GOOGLE_SERVICE_ACCOUNT_FILEenvironment variable
- Verify the service account email has been added to the Drive folder
- Ensure the folder ID is correct
- Check that the Drive API is enabled
- Add your frontend domain to
CORS_ORIGINSinconfig.py - Ensure the Flask server is running and accessible
| Variable | Description | Default |
|---|---|---|
GOOGLE_SERVICE_ACCOUNT_FILE |
Path to service account JSON | backend/service-account.json |
GOOGLE_DRIVE_MAIN_FOLDER_ID |
Main Google Drive folder ID | (empty) |
FLASK_HOST |
Flask server host | 0.0.0.0 |
FLASK_PORT |
Flask server port | 5000 |
FLASK_DEBUG |
Debug mode | False |
LOG_LEVEL |
Logging level | INFO |
For issues or questions, contact:
- Email: support@tradeflow.com
- Documentation: https://docs.tradeflow.com