Skip to content

Commit 78a2548

Browse files
committed
added google drive support
1 parent 111e516 commit 78a2548

12 files changed

Lines changed: 3148 additions & 131 deletions

File tree

.coverage

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ wheels/
1717

1818
# env files
1919
.env
20+
21+
# secrets
22+
client_secret.json
23+
google_drive_credentials.json

README.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ See [MCP Use Cases](#for-mcp-servers-model-context-protocol) for detailed exampl
9393
- **Pyodide Provider**: Web browser filesystem integration
9494
- **S3 Provider**: Cloud storage with AWS S3 or S3-compatible services
9595
- **E2B Sandbox Provider**: Remote sandbox environment filesystem
96+
- **Google Drive Provider**: Store files in user's Google Drive (user owns data!)
9697
- Easy to extend with custom providers
9798

9899
### 🔒 Advanced Security
@@ -138,6 +139,9 @@ pip install chuk-virtual-fs
138139
# Install with S3 support
139140
pip install "chuk-virtual-fs[s3]"
140141

142+
# Install with Google Drive support
143+
pip install "chuk-virtual-fs[google_drive]"
144+
141145
# Install with WebDAV mounting support (recommended!)
142146
pip install "chuk-virtual-fs[webdav]"
143147

@@ -148,6 +152,8 @@ pip install "chuk-virtual-fs[mount]"
148152
pip install "chuk-virtual-fs[all]"
149153

150154
# Using uv
155+
uv pip install "chuk-virtual-fs[s3]"
156+
uv pip install "chuk-virtual-fs[google_drive]"
151157
uv pip install "chuk-virtual-fs[webdav]"
152158
uv pip install "chuk-virtual-fs[mount]"
153159
uv pip install "chuk-virtual-fs[all]"
@@ -238,6 +244,7 @@ The virtual filesystem supports multiple storage providers:
238244
- **Memory**: In-memory storage (default)
239245
- **SQLite**: SQLite database storage
240246
- **S3**: AWS S3 or S3-compatible storage
247+
- **Google Drive**: User's Google Drive (user owns data!)
241248
- **Pyodide**: Native integration with Pyodide environment
242249
- **E2B**: E2B Sandbox environments
243250

@@ -348,6 +355,172 @@ To use the E2B Sandbox Provider, you need to:
348355

349356
Note: You can obtain an E2B API key from the [E2B platform](https://e2b.dev).
350357

358+
### Google Drive Provider
359+
360+
The Google Drive provider lets you store files in the user's own Google Drive. This approach offers unique advantages:
361+
362+
-**User Owns Data**: Files are stored in the user's Google Drive, not your infrastructure
363+
-**Natural Discoverability**: Users can view/edit files directly in Google Drive UI
364+
-**Built-in Sharing**: Use Drive's native sharing and collaboration features
365+
-**Cross-Device Sync**: Files automatically sync across all user devices
366+
-**No Infrastructure Cost**: No need to manage storage servers or buckets
367+
368+
#### Installation
369+
370+
```bash
371+
# Install with Google Drive support
372+
pip install "chuk-virtual-fs[google_drive]"
373+
374+
# Or with uv
375+
uv pip install "chuk-virtual-fs[google_drive]"
376+
```
377+
378+
#### OAuth Setup
379+
380+
Before using the Google Drive provider, you need to set up OAuth2 credentials:
381+
382+
**Step 1: Create Google Cloud Project**
383+
384+
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
385+
2. Create a new project (or select existing)
386+
3. Enable the Google Drive API
387+
4. Go to "Credentials" → Create OAuth 2.0 Client ID
388+
5. Choose "Desktop app" as application type
389+
6. Download the JSON file and save as `client_secret.json`
390+
391+
**Step 2: Run OAuth Setup**
392+
393+
```bash
394+
# Run the OAuth setup helper
395+
python examples/providers/google_drive_oauth_setup.py
396+
397+
# Or with custom client secrets file
398+
python examples/providers/google_drive_oauth_setup.py --client-secrets /path/to/client_secret.json
399+
```
400+
401+
This will:
402+
- Open a browser for Google authorization
403+
- Save credentials to `google_drive_credentials.json`
404+
- Show you the configuration for Claude Desktop / MCP servers
405+
406+
#### Example Usage
407+
408+
```python
409+
import json
410+
from pathlib import Path
411+
from chuk_virtual_fs import AsyncVirtualFileSystem
412+
413+
# Load credentials from OAuth setup
414+
with open("google_drive_credentials.json") as f:
415+
credentials = json.load(f)
416+
417+
# Create filesystem with Google Drive provider
418+
async with AsyncVirtualFileSystem(
419+
provider="google_drive",
420+
credentials=credentials,
421+
root_folder="CHUK", # Creates /CHUK/ folder in Drive
422+
cache_ttl=60 # Cache file IDs for 60 seconds
423+
) as fs:
424+
# Create project structure
425+
await fs.mkdir("/projects/demo")
426+
427+
# Write files - they appear in Google Drive!
428+
await fs.write_file(
429+
"/projects/demo/README.md",
430+
"# My Project\n\nFiles stored in Google Drive!"
431+
)
432+
433+
# Read files back
434+
content = await fs.read_file("/projects/demo/README.md")
435+
436+
# List directory
437+
files = await fs.ls("/projects/demo")
438+
439+
# Get file metadata
440+
info = await fs.get_node_info("/projects/demo/README.md")
441+
print(f"Size: {info.size} bytes")
442+
print(f"Modified: {info.modified_at}")
443+
444+
# Files are now in Google Drive under /CHUK/projects/demo/
445+
```
446+
447+
#### Configuration for Claude Desktop
448+
449+
After running OAuth setup, add to your `claude_desktop_config.json`:
450+
451+
```json
452+
{
453+
"mcpServers": {
454+
"vfs": {
455+
"command": "uvx",
456+
"args": ["chuk-virtual-fs"],
457+
"env": {
458+
"VFS_PROVIDER": "google_drive",
459+
"GOOGLE_DRIVE_CREDENTIALS": "{\"token\": \"...\", \"refresh_token\": \"...\", ...}"
460+
}
461+
}
462+
}
463+
}
464+
```
465+
466+
(The OAuth setup helper generates the complete configuration)
467+
468+
#### Features
469+
470+
- **Two-Level Caching**: Path→file_id and file_id→metadata caches for performance
471+
- **Metadata Storage**: Session IDs, custom metadata, and tags stored in Drive's `appProperties`
472+
- **Async Operations**: Full async/await support using `asyncio.to_thread`
473+
- **Standard Operations**: All VirtualFileSystem methods work (mkdir, write_file, read_file, ls, etc.)
474+
- **Statistics**: Track API calls, cache hits/misses with `get_storage_stats()`
475+
476+
#### Provider-Specific Parameters
477+
478+
```python
479+
from chuk_virtual_fs.providers import GoogleDriveProvider
480+
481+
provider = GoogleDriveProvider(
482+
credentials=credentials_dict, # OAuth2 credentials
483+
root_folder="CHUK", # Root folder name in Drive
484+
cache_ttl=60, # Cache TTL in seconds (default: 60)
485+
session_id="optional_session_id", # Optional session tracking
486+
sandbox_id="default" # Optional sandbox tracking
487+
)
488+
```
489+
490+
#### Examples
491+
492+
See the `examples/providers/` directory for complete examples:
493+
494+
- **`google_drive_oauth_setup.py`**: Interactive OAuth2 setup helper
495+
- **`google_drive_example.py`**: Comprehensive end-to-end example
496+
497+
Run the full example:
498+
499+
```bash
500+
# First, set up OAuth credentials
501+
python examples/providers/google_drive_oauth_setup.py
502+
503+
# Then run the example
504+
python examples/providers/google_drive_example.py
505+
```
506+
507+
#### How It Works
508+
509+
1. **OAuth2 Authentication**: Uses Google's OAuth2 flow for secure authorization
510+
2. **Root Folder**: Creates a folder (default: `CHUK`) in the user's Drive as the filesystem root
511+
3. **Path Mapping**: Virtual paths like `/projects/demo/file.txt``CHUK/projects/demo/file.txt` in Drive
512+
4. **Metadata**: Custom metadata (session_id, tags, etc.) stored in Drive's `appProperties`
513+
5. **Caching**: Two-level cache reduces API calls for better performance
514+
515+
#### Use Cases
516+
517+
Perfect for:
518+
- **User-Owned Workspaces**: Give users their own persistent workspace in their Drive
519+
- **Collaborative AI Projects**: Users can share their Drive folders with collaborators
520+
- **Long-Term Storage**: User controls retention and can access files outside your app
521+
- **Cross-Device Access**: Users access their files from any device with Drive
522+
- **Zero Infrastructure**: No need to run storage servers or manage buckets
523+
351524
## 🛡️ Security Features
352525

353526
The virtual filesystem provides robust security features to protect against common vulnerabilities and limit resource usage.

0 commit comments

Comments
 (0)