A Python script to interact with Google Drive for listing folders, uploading local folders, and creating backups using the Google Drive API.
- Python: Version 3.6 or higher
- Google Cloud Project: Set up a project at Google Cloud Console
- Enable the Google Drive API
- Create OAuth 2.0 credentials (download
credentials.json)
- Dependencies: Listed in
requirements.txt(installed via pip in virtual environment)
- Clone or download the repository.
- Create a virtual environment:
python3 -m venv venv - Activate the virtual environment:
source venv/bin/activate - Install dependencies:
pip install -r requirements.txt
- Obtain
credentials.jsonfrom your Google Cloud Console and place it in the project directory. - Edit
drive_manager.pyand update theLOCAL_FOLDER_PATHvariable to the path of the local folder you want to upload or back up (e.g.,/home/user/my_folder).
Run the script from the command line with one of the following commands:
-
List folders: Lists folders in the root of your Google Drive.
python drive_manager.py list -
Upload folder: Uploads the folder specified in
LOCAL_FOLDER_PATHto the root of your Google Drive.python drive_manager.py upload -
Create backup: Creates a timestamped backup folder on Google Drive and uploads the contents of
LOCAL_FOLDER_PATHinto it.python drive_manager.py backup
On the first run, the script will open a browser for OAuth authorization. Follow the prompts to grant access.
Run the test suite with:
python -m pytest
Or
python test_drive_manager.py
- The script uses OAuth for secure authentication.
- Uploads handle folders recursively, creating subfolders as needed.
- Backups are stored in timestamped folders (e.g.,
Backup_20231227_120000). - Ensure your Google account has sufficient storage space.
- For issues, check the Google Drive API documentation or verify your credentials.
See Issue #1 for integration into LaTeX DocOps module.