A standalone Python CLI application to upload local files to an arbitrary path on Google Drive. It handles Google Drive authentication, verifies path existence, creates missing folders, and supports resumable uploads.
This application acts as a bridge between your local file system and the Google Drive API, handling the complexity of OAuth 2.0 and path resolution.
- Authentication: Securely loads user credentials and maintains a persistent session via
token.json. - Path Resolution: Intelligently parses the destination path (
A/B/C). It checks each folder level, reusing existing folders and creating new ones only when necessary (deduplication). - Resumable Upload: Uses the robust
MediaFileUploadprotocol to handle large files and unstable connections.
graph TD
User([User]) -->|1. Run Command| CLI[google_drive_upload.py]
subgraph "Local Environment"
CLI -->|2. Check Auth| Auth[auth.py]
Auth -->|Read| Creds[credentials.json]
Auth -->|Read/Write| Token[token.json]
CLI -->|3. Read File| LocalFile[Source File]
end
subgraph "Google Cloud"
CLI -->|4. API Calls| DriveAPI[Google Drive API]
DriveAPI -->|5. Deduplicate| Folder[Remote Folder]
DriveAPI -->|6. Upload| File[Remote File]
end
.
├── google_drive_upload.py # Main Entry Point
├── auth.py # Authentication Handling
├── drive_utils.py # Drive API Logic (Folder creation, Uploads)
├── credentials.json # OAuth Client ID (User provided)
├── token.json # Active Session (Auto-generated)
├── Vagrantfile # Headless testing environment
├── requirements.txt # Python dependencies
└── README.md # This file
- Python 3.7+
- A Google Cloud Project with the Google Drive API enabled.
- An OAuth 2.0 Client ID (JSON file) downloaded from the Google Cloud Console.
Clone or extract the project archive, then install dependencies:
# Optional: Create virtual environment
python3 -m venv venv
source venv/bin/activate # Linux/Mac
# .\venv\Scripts\activate # Windows
pip install -r requirements.txtBefore using the tool, you must set up a project in Google Cloud to get your credentials.json.
- Create a Project: Go to Google Cloud Console and create a new project.
- Enable API: Search for "Google Drive API" in the Library and enable it.
- Configure OAuth:
- Go to OAuth consent screen, select External, and add your email as a Test User.
- Create Keys:
- Go to Credentials > Create Credentials > OAuth client ID.
- Select Application type: Desktop app.
- Download the JSON, rename it to
credentials.json, and place it in the project root.
First, create a dummy file:
echo "Hello World" > test.txtUpload test.txt to the root of Google Drive:
python3 google_drive_upload.py --source-file test.txtUpload test.txt to a specific folder. The CLI will automatically create folders Headless and Test if they don't exist.
python3 google_drive_upload.py --source-file test.txt --destination-path "Headless/Test"The application supports a Console-Based Authentication Flow designed specifically for headless servers (like Ubuntu Server) where no browser is available.
- Run the Script: Execute the command on the server.
python3 google_drive_upload.py ...
- Follow the Prompt: The script will detect it cannot open a browser and will print a URL.
- Authorize:
- Copy the URL to a browser on your local machine (laptop/desktop).
- Log in and approve the app.
- Copy the verification code provided by Google.
- Complete Login: Paste the code back into the server terminal. The script will save the session to
token.json.
(Alternative: You can still generate token.json locally and copy it to the server if you prefer, but the console flow above is recommended).
This repository includes a Vagrantfile to test the application in a pristine Ubuntu 22.04 environment.
-
Start VM:
vagrant up
Note: This will provision the VM and install Python dependencies.
-
SSH into VM:
vagrant ssh
-
Test Upload:
# Follow the authentication link if prompted (token.json will be generated) cd /vagrant # Create the test file echo "Hello Headless World" > test.txt python3 google_drive_upload.py --source-file test.txt --destination-path "Headless/Test"