Cloudflare Worker that watches a Google Drive folder for changes via webhooks, queues events, and syncs a complete file listing (name + ID) to a Google Sheet.
Google Drive push notification
│
▼
┌───────────────────────┐
│ Worker POST /webhook│──► CF Queue (batched)
│ POST /setup │ │
│ POST /stop │ ▼
│ POST /reindex│ Queue consumer:
│ GET / │ reindex folder → write Sheet
└───────────────────────┘
▲
│
Cron (every 5 days) — renews watch channel (if WATCH_ENABLED)
-
Go to Google Cloud Console and create a project (or use an existing one).
-
Enable APIs — go to APIs & Services → Library and enable:
- Google Drive API
- Google Sheets API
-
Create a Service Account:
- Go to APIs & Services → Credentials
- Click Create Credentials → Service Account
- Name it (e.g.
drive-index-worker) - No need to grant project-level roles
- Click Done
-
Create a key for the Service Account:
- Click on the service account you just created
- Go to the Keys tab
- Add Key → Create new key → JSON
- Download the JSON file — you'll need its contents as a secret
-
Share the Drive folder with the service account:
- Copy the service account email (e.g.
drive-index-worker@project.iam.gserviceaccount.com) - Open the Google Drive folder you want to index
- Click Share and add the service account email with Viewer access
- Copy the service account email (e.g.
-
Share the Google Sheet with the service account:
- Create a new Google Sheet (or use an existing one)
- Share it with the service account email with Editor access
- Note the spreadsheet ID from the URL:
https://docs.google.com/spreadsheets/d/<SPREADSHEET_ID>/edit
-
Install Wrangler (if not already):
pnpm install -g wrangler wrangler login
-
Create the Queue:
wrangler queues create drive-index-events
-
Set secrets (deploy will fail if any are missing):
# Paste the entire JSON key file contents when prompted wrangler secret put GOOGLE_SERVICE_ACCOUNT_KEY # Generate a random secret for webhook verification (use: openssl rand -hex 32) wrangler secret put WEBHOOK_SECRET # Google Drive folder ID (from URL: drive.google.com/drive/folders/<FOLDER_ID>) wrangler secret put FOLDER_ID # Google Sheet ID (from URL: docs.google.com/spreadsheets/d/<SPREADSHEET_ID>/edit) wrangler secret put SPREADSHEET_ID # Sheet tab name where file index will be written wrangler secret put SHEET_NAME # Public URL of the deployed worker wrangler secret put WORKER_URL
Alternatively, set them all at once:
echo '{"FOLDER_ID":"...","SPREADSHEET_ID":"...","SHEET_NAME":"...","WORKER_URL":"..."}' | wrangler secret bulk
-
Enable watching (optional) — set
WATCH_ENABLEDto"true"inwrangler.jsoncif you want the cron to auto-renew the Drive watch channel. -
Deploy:
pnpm deploy
Once deployed, register the webhook so Google Drive sends push notifications:
curl -X POST https://drive-index.<your-subdomain>.workers.dev/setup \
-H "Authorization: Bearer <your-webhook-secret>"This creates a watch channel that expires in ~7 days. The cron trigger (every 5 days) automatically renews it while WATCH_ENABLED is "true".
Set WATCH_ENABLED to "false" in wrangler.jsonc and redeploy (or update via the CF dashboard). The cron will stop renewing the channel and it will expire within 7 days.
To trigger a full reindex on demand:
curl -X POST https://drive-index.<your-subdomain>.workers.dev/reindex \
-H "Authorization: Bearer <your-webhook-secret>"pnpm install
# Interactive prompt — reads secrets.required from wrangler.jsonc
pnpm setup:local
pnpm devpnpm test-
Webhook — Google Drive sends a POST to
/webhookwhen files change in the watched folder. The worker verifies the request using the channel token and enqueues an event. -
Queue consumer — CF Queue batches events (max 50 or 30s timeout). The consumer recursively lists all files in the folder and its subfolders via the Drive API, then replaces the sheet contents (from row 2 down) with file names in column A and IDs in column B.
-
Cron — Every 5 days, the scheduled handler renews the Drive watch channel (channels expire after ~7 days max). Only runs when
WATCH_ENABLEDis"true". -
Reindex —
POST /reindextriggers a manual full reindex without going through the queue.
| Method | Path | Auth | Description |
|---|---|---|---|
GET |
/ |
None | Health check + watch status |
POST |
/webhook |
x-goog-channel-token |
Google Drive push notification receiver |
POST |
/setup |
Bearer <secret> |
Register Drive watch channel |
POST |
/stop |
Bearer <secret> |
Instructions to stop watching |
POST |
/reindex |
Bearer <secret> |
Trigger full reindex immediately |
MIT