Skip to content

briannalytical/google-drive-github-auto-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Google Drive to GitHub Auto-Sync

Automatically sync images from Google Drive to GitHub with version control and automatic release notifications.

What This Does

  • 🔄 Automatically syncs images from a Google Drive folder to this GitHub repository
  • 📅 Runs daily via GitHub Actions (customizable schedule)
  • 🔔 Creates releases automatically when new images are added
  • 📊 Tracks changes via Git commit history
  • 🗂️ Preserves folder structure from your Google Drive

How It Works

  1. GitHub Actions runs on a schedule (default: daily at 2 AM UTC)
  2. Connects to Google Drive using a service account
  3. Downloads any new or updated images
  4. Commits them to the repository
  5. Creates a GitHub Release to notify watchers
  6. Repeat!

Setup Guide

Prerequisites

  • A Google account with Google Drive
  • A GitHub account
  • Images stored in a Google Drive folder

Part 1: Google Cloud Setup (15 minutes)

1.1 Create a Google Cloud Project

  1. Go to Google Cloud Console
  2. Click the project dropdown at the top
  3. Click "NEW PROJECT"
  4. Name it (e.g., "github-sync")
  5. Click "CREATE"

1.2 Enable Google Drive API

  1. In the search bar, type "Google Drive API"
  2. Click on "Google Drive API"
  3. Click "ENABLE"

1.3 Create Service Account

  1. Go to "IAM & Admin""Service Accounts" (use the left sidebar or search)
  2. Click "+ CREATE SERVICE ACCOUNT"
  3. Fill in:
    • Service account name: github-sync (or whatever you prefer)
    • Service account ID: Auto-fills, leave it
  4. Click "CREATE AND CONTINUE"
  5. Skip the optional steps (click "CONTINUE" then "DONE")

1.4 Create Service Account Key (JSON)

  1. Click on the service account you just created
  2. Go to the "KEYS" tab
  3. Click "ADD KEY""Create new key"
  4. Choose "JSON" format
  5. Click "CREATE"
  6. Save the downloaded JSON file - you'll need it later!

1.5 Get Your Service Account Email

While still on this page, copy the service account email at the top.

It looks like: github-sync@your-project-name.iam.gserviceaccount.com

You'll need this in the next section!


Part 2: Google Drive Setup (5 minutes)

2.1 Get Your Folder ID

  1. Go to Google Drive
  2. Navigate to the folder you want to sync
  3. Look at the URL in your browser:
    https://drive.google.com/drive/folders/YOUR_FOLDER_ID_HERE
    
  4. Copy everything after /folders/ - this is your folder ID

Example: If the URL is https://drive.google.com/drive/folders/143UQ42Vk1TBqQ7pT-5HTfUG8-RHr4bos

Then your folder ID is: 143UQ42Vk1TBqQ7pT-5HTfUG8-RHr4bos

2.2 Share Folder with Service Account

  1. Right-click on the folder you want to sync
  2. Click "Share"
  3. In the "Add people and groups" field, paste your service account email
    • (e.g., github-sync@your-project-name.iam.gserviceaccount.com)
  4. Set permission to "Viewer"
  5. Uncheck "Notify people"
  6. Click "Share"

Alternative: If you get an error, make the folder "Anyone with the link" instead:

  • Click "Change" next to "Restricted"
  • Select "Anyone with the link"
  • Set to "Viewer"

Part 3: GitHub Repository Setup (10 minutes)

3.1 Create Repository (if you haven't already)

  1. Go to GitHub
  2. Click "New repository"
  3. Name it (e.g., "image-collection")
  4. Choose Private or Public
  5. Click "Create repository"

3.2 Add Files to Repository

Upload these files to your repository:

  • sync_drive_images.py
  • requirements.txt
  • .gitignore
  • .github/workflows/sync-images.yml
  • README.md

You can drag and drop them via the GitHub web interface.

3.3 Add GitHub Secrets

  1. Go to your repository's Settings
  2. Click "Secrets and variables""Actions"
  3. Click "New repository secret"

First Secret:

  • Name: GOOGLE_SERVICE_ACCOUNT_JSON
  • Value: Open the JSON file you downloaded earlier, copy ALL the contents, paste here
  • Click "Add secret"

Second Secret:

  • Name: DRIVE_FOLDER_ID
  • Value: The folder ID you copied from the Drive URL
  • Click "Add secret"

Part 4: Initial Sync (20-40 minutes depending on file count)

Important: The first sync should be done locally on your computer to avoid GitHub server timeouts when pushing hundreds of files at once.

4.1 Clone Your Repository

git clone https://github.com/your-username/your-repo.git
cd your-repo

4.2 Install Python Dependencies

# macOS/Linux
pip3 install google-api-python-client google-auth-httplib2 google-auth-oauthlib

# Windows
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

4.3 Set Environment Variables

On macOS/Linux:

export DRIVE_FOLDER_ID="your-folder-id-here"
export GOOGLE_SERVICE_ACCOUNT_JSON='paste-entire-json-contents-here'

On Windows (PowerShell):

$env:DRIVE_FOLDER_ID="your-folder-id-here"
$env:GOOGLE_SERVICE_ACCOUNT_JSON='paste-entire-json-contents-here'

4.4 Run the Sync Script

# macOS/Linux
python3 sync_drive_images.py

# Windows
python sync_drive_images.py

This will download all images to the images/ folder.

4.5 Commit and Push

git add images/ manifest.json .gitattributes
git commit -m "Initial sync: Add all images from Google Drive"
git push

If the push fails with HTTP 500 errors (too many files), push in batches:

# Add and commit 100 files at a time
git add $(git ls-files --others images/ | head -100)
git commit -m "Initial sync batch 1"
git push

# Repeat for more batches
git add $(git ls-files --others images/ | head -100)
git commit -m "Initial sync batch 2"
git push

Usage

Automatic Daily Syncs

After the initial setup, GitHub Actions will automatically:

  1. Run every day at 2 AM UTC
  2. Check Google Drive for new/updated images
  3. Download and commit any changes
  4. Create a release if new images were added

You don't need to do anything - it runs automatically!

Manual Sync

To trigger a sync manually:

  1. Go to the Actions tab in your repository
  2. Click "Sync Google Drive Images"
  3. Click "Run workflow"
  4. Click the green "Run workflow" button

Change the Schedule

To change when automatic syncs run:

  1. Edit .github/workflows/sync-images.yml
  2. Find the line: cron: '0 2 * * *'
  3. Change it to your preferred schedule:
# Every 6 hours
- cron: '0 */6 * * *'

# Every Monday at 9 AM
- cron: '0 9 * * 1'

# Twice daily (2 AM and 2 PM)
- cron: '0 2,14 * * *'

Inviting Collaborators

Give People Access to Your Private Repo

  1. Go to SettingsCollaborators
  2. Click "Add people"
  3. Enter their GitHub username or email
  4. Choose permission level:
    • Read: Can view and clone (recommended for viewers)
    • Write: Can push changes
    • Admin: Full control

How Collaborators Clone the Repo

After you invite them, they can:

git clone https://github.com/your-username/your-repo.git
cd your-repo

To pull new updates:

git pull

Getting Notifications

Automatic Releases

Every time new images are added, the workflow automatically creates a GitHub Release with:

  • A timestamp
  • Number of new images added
  • Number of updated images
  • Link to the commit with changes

How to Get Notified

Option 1: Watch Releases Only (Recommended)

  1. Click "Watch" at the top of the repository
  2. Select "Custom"
  3. Check "Releases" only
  4. Click "Apply"

Now you'll get a notification whenever new images are added!

Option 2: Use RSS Feed

Subscribe to the releases RSS feed in any RSS reader:

https://github.com/your-username/your-repo/releases.atom

Option 3: Email Notifications

GitHub will email you about releases if you have "Watch: Releases" enabled.


File Structure

After syncing, your repository will look like this:

your-repo/
├── .github/
│   └── workflows/
│       └── sync-images.yml       # GitHub Actions workflow
├── images/                        # All synced images (matches Drive structure)
│   ├── Folder 1/
│   │   ├── image1.png
│   │   └── image2.jpg
│   └── Folder 2/
│       └── image3.png
├── sync_drive_images.py          # Sync script
├── requirements.txt               # Python dependencies
├── manifest.json                  # Tracks synced files (auto-generated)
├── .gitignore                     # Ignores sensitive files
└── README.md                      # This file

The images/ folder structure exactly mirrors your Google Drive folder structure.


Troubleshooting

"File not found" Error (404)

Problem: The folder ID is wrong or the service account doesn't have access.

Solutions:

  1. Double-check your DRIVE_FOLDER_ID - make sure you copied the complete folder ID from the URL
  2. Verify the folder is shared with your service account email
  3. Try making the folder "Anyone with the link" instead

"HTTP 500" Push Errors

Problem: Too many files being pushed at once.

Solution: For the first sync, always do it locally and push in batches (see Part 4.5 above).

"Permission denied" Error

Problem: The service account doesn't have access to the folder.

Solution: Make sure you shared the folder with the exact service account email from Google Cloud.

Workflow Doesn't Run

Problem: GitHub Actions might be disabled.

Solutions:

  1. Go to SettingsActionsGeneral
  2. Make sure Actions are enabled
  3. Check that the workflow file is in .github/workflows/ (not github/workflows/)

Images Not Appearing

Problem: Push succeeded but images aren't visible.

Solutions:

  1. Hard refresh your browser (Ctrl+Shift+R or Cmd+Shift+R)
  2. Check the commits - click on the latest commit to see what files were added
  3. Look for the images/ folder in your repository

Important Notes

GitHub Limits

  • File size limit: 100 MB per file
  • Repository size: Soft limit ~1 GB, hard limit 5 GB
  • For very large collections, consider using Git LFS

Security

  • ✅ Service account JSON is stored securely in GitHub Secrets
  • ✅ Never commit the JSON file to your repository
  • ✅ The .gitignore file prevents accidental commits

Costs

  • Google Drive API: Free (up to 20,000 requests/day)
  • GitHub Actions:
    • Public repos: Unlimited minutes
    • Private repos: 2,000 minutes/month free

Support

If you encounter issues:

  1. Check the Actions tab for error logs
  2. Verify your secrets are set correctly
  3. Make sure the folder is shared with the service account
  4. Double-check the folder ID is complete

What's Next?

After setup is complete:

  • ✅ Your images sync automatically every day
  • ✅ Collaborators get notified via releases
  • ✅ Full version history via Git commits
  • ✅ Just add images to Google Drive and forget about it!

Enjoy your automated image collection! 🎉

About

for migrating folders via the google drive API to a github repo

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages