Skip to content

hwong5208/python_gdrive_cli

Repository files navigation

Google Drive Upload CLI

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.

Architecture

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.

Application Logic

  1. Authentication: Securely loads user credentials and maintains a persistent session via token.json.
  2. 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).
  3. Resumable Upload: Uses the robust MediaFileUpload protocol to handle large files and unstable connections.

Architecture Diagram

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
Loading

Directory Structure

.
├── 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

Prerequisites

  • 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.

Setup & Installation

1. Project Setup

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.txt

2. Setting up Credentials

Before using the tool, you must set up a project in Google Cloud to get your credentials.json.

  1. Create a Project: Go to Google Cloud Console and create a new project.
  2. Enable API: Search for "Google Drive API" in the Library and enable it.
  3. Configure OAuth:
    • Go to OAuth consent screen, select External, and add your email as a Test User.
  4. 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.

Usage

Basic Upload

First, create a dummy file:

echo "Hello World" > test.txt

Upload test.txt to the root of Google Drive:

python3 google_drive_upload.py --source-file test.txt

Upload to Specific Path

Upload 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"

Headless / Server Environment (Authentication)

The application supports a Console-Based Authentication Flow designed specifically for headless servers (like Ubuntu Server) where no browser is available.

  1. Run the Script: Execute the command on the server.
    python3 google_drive_upload.py ...
  2. Follow the Prompt: The script will detect it cannot open a browser and will print a URL.
  3. 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.
  4. 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).

Validation & Testing

Vagrant (Pristine Environment)

This repository includes a Vagrantfile to test the application in a pristine Ubuntu 22.04 environment.

  1. Start VM:

    vagrant up

    Note: This will provision the VM and install Python dependencies.

  2. SSH into VM:

    vagrant ssh
  3. 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"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages