Google Drive Skill
Resolve SKILL_SCRIPTS
IMPORTANT: All commands below use SKILL_SCRIPTS as shorthand for the absolute path to the scripts/ directory of this skill. Resolve it before running any script:
CODEBLOCK0
Overview
This skill provides patterns and ready-to-run scripts for CRUD operations on Google Drive using the Drive API v3.
| Script | Operation | Auth Required |
|---|
| INLINECODE3 | List files/folders in a folder | API key (public) or service account |
| INLINECODE4 |
Download a file to disk | API key (public) or service account |
|
create_folder.py | Create a new folder | Service account |
|
upload_file.py | Upload a local file | Service account |
|
update_file.py | Rename, move, or replace content | Service account |
|
delete_file.py | Trash or permanently delete | Service account |
|
set_permissions.py | Make public / share / revoke | Service account |
Supporting files:
- -
scripts/drive_client.py — shared auth factory (imported by all scripts) - INLINECODE11 — template for your service account JSON
- INLINECODE12 — MIME type quick reference
- INLINECODE13 — HTTP error codes + retry patterns
Quick Start
1. Install dependencies
CODEBLOCK1
2. Set credentials
CODEBLOCK2
See assets/service_account_template.json for the expected JSON structure.
3. Resolve SKILL_SCRIPTS (see top of this file), then run scripts
Script Commands
List files in a folder
CODEBLOCK3
Download a file
CODEBLOCK4
Create a folder
CODEBLOCK5
Upload a file
CODEBLOCK6
Update a file
CODEBLOCK7
Delete or trash a file
CODEBLOCK8
Manage permissions
CODEBLOCK9
Auth Setup
Option A — API Key (read-only public files)
Use when the file/folder is shared as "Anyone with the link" and you only need to read.
CODEBLOCK10
Option B — Service Account (read + write on shared/public drives)
Use when you need to create, modify, or delete files.
CODEBLOCK11
Copy assets/service_account_template.json to see the expected structure.
Store the real file outside the repo and never commit it.
Add service_account.json to .gitignore.
File & Folder ID
Every Drive resource has a unique fileId. Extract it from the share URL:
CODEBLOCK12
CRUD Operations
List Files in a Public Folder
CODEBLOCK13
Download a Public File
CODEBLOCK14
Create a Folder
CODEBLOCK15
Upload / Create a File
CODEBLOCK16
Upload from In-Memory Bytes
CODEBLOCK17
Update File Metadata (rename, move)
CODEBLOCK18
Update File Content
CODEBLOCK19
Delete a File or Folder
CODEBLOCK20
Deleting a folder also deletes all its children permanently. Prefer trashing:
CODEBLOCK21
Set Permissions (Make Public)
To make a file publicly readable (anyone with the link):
CODEBLOCK22
To grant write access to a specific user:
CODEBLOCK23
Common MIME Types
| Content | MIME Type |
|---|
| Folder | INLINECODE20 |
| Google Doc |
application/vnd.google-apps.document |
| Google Sheet |
application/vnd.google-apps.spreadsheet |
| PDF |
application/pdf |
| Plain text |
text/plain |
| JSON |
application/json |
| PNG |
image/png |
| ZIP |
application/zip |
Error Handling
CODEBLOCK24
Common HTTP status codes:
- -
400 — bad request (check field names, MIME types) - INLINECODE29 — permission denied (check auth scope or file sharing settings)
- INLINECODE30 — file not found (check
fileId, supportsAllDrives) - INLINECODE33 — rate limited (back off and retry)
Required Python Packages
CODEBLOCK25
Install:
pip install google-api-python-client google-auth google-auth-httplib2
Security Notes
- - Never hardcode API keys or service account credentials in source code. Use environment variables or secret managers.
- Restrict API key to the Drive API scope in the Google Cloud Console.
- Service account credentials (
service_account.json) must be in .gitignore. - When sharing files, prefer time-limited access tokens over permanent public links where possible.
- INLINECODE36 with
"role": "writer" on a production folder is dangerous — audit permissions regularly.
Reference
Local references (this skill):
Official docs:
Google Drive 技能
解析 SKILL_SCRIPTS
重要提示: 以下所有命令均使用 SKILL_SCRIPTS 作为本技能 scripts/ 目录绝对路径的简写。在运行任何脚本之前,请先解析该路径:
bash
SKILL_SCRIPTS=$(
wsroot=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
wspath=$wsroot/.github/skills/google-drive-skill/scripts
globalpath=$HOME/.openclaw/workspace/skills/google-drive-skill/scripts
globalpath2=$HOME/.openclaw/skills/google-drive-skill/scripts
if [ -d $wspath ]; then
echo $wspath
elif [ -d $globalpath ]; then
echo $globalpath
elif [ -d $globalpath2 ]; then
echo $globalpath2
else
find $HOME /opt -type d -name google-drive-skill -path /skills/ 2>/dev/null \
| head -1 | sed s|$|/scripts|
fi
)
echo SKILLSCRIPTS=$SKILLSCRIPTS
概述
本技能提供使用 Drive API v3 对 Google Drive 进行 CRUD 操作的模式和即用型脚本。
| 脚本 | 操作 | 需要认证 |
|---|
| listfiles.py | 列出文件夹中的文件/文件夹 | API 密钥(公开)或服务账号 |
| downloadfile.py |
将文件下载到磁盘 | API 密钥(公开)或服务账号 |
| create_folder.py | 创建新文件夹 | 服务账号 |
| upload_file.py | 上传本地文件 | 服务账号 |
| update_file.py | 重命名、移动或替换内容 | 服务账号 |
| delete_file.py | 移至回收站或永久删除 | 服务账号 |
| set_permissions.py | 设为公开/共享/撤销权限 | 服务账号 |
支持文件:
- - scripts/driveclient.py — 共享认证工厂(所有脚本导入)
- assets/serviceaccounttemplate.json — 服务账号 JSON 模板
- references/mimetypes.md — MIME 类型快速参考
- references/error_codes.md — HTTP 错误代码 + 重试模式
快速开始
1. 安装依赖
bash
pip install google-api-python-client google-auth google-auth-httplib2
2. 设置凭据
bash
只读(公开文件):
export GOOGLE
APIKEY=your
apikey_here
读写(服务账号):
export GOOGLE
SERVICEACCOUNT
JSON=/path/to/serviceaccount.json
参见 assets/serviceaccounttemplate.json 了解预期的 JSON 结构。
3. 解析 SKILL_SCRIPTS(参见本文件顶部),然后运行脚本
脚本命令
列出文件夹中的文件
bash
表格输出(默认):
python $SKILL
SCRIPTS/listfiles.py --folder-id FOLDER_ID
JSON 输出:
python $SKILL
SCRIPTS/listfiles.py --folder-id FOLDER_ID --json
下载文件
bash
保存到指定路径:
python $SKILL
SCRIPTS/downloadfile.py --file-id FILE_ID --dest ./downloads/report.pdf
保存到目录(从 Drive 自动获取文件名):
python $SKILL
SCRIPTS/downloadfile.py --file-id FILE_ID --dest ./downloads/
创建文件夹
bash
在 Drive 根目录下:
python $SKILL
SCRIPTS/createfolder.py --name 我的新文件夹
在指定父文件夹内:
python $SKILL
SCRIPTS/createfolder.py --name 子文件夹 --parent-id PARENT
FOLDERID
上传文件
bash
基本上传(MIME 类型自动检测):
python $SKILL
SCRIPTS/uploadfile.py --src ./report.pdf --parent-id FOLDER_ID
在 Drive 上自定义名称:
python $SKILL
SCRIPTS/uploadfile.py --src ./report.pdf --name Q1-Report.pdf --parent-id FOLDER_ID
上传并立即设为公开可读:
python $SKILL
SCRIPTS/uploadfile.py --src ./photo.png --parent-id FOLDER_ID --make-public
更新文件
bash
重命名:
python $SKILL
SCRIPTS/updatefile.py --file-id FILE_ID --name 新名称.pdf
移动到不同文件夹:
python $SKILL
SCRIPTS/updatefile.py --file-id FILE
ID --move-to NEWPARENT
ID --old-parent OLDPARENT_ID
替换文件内容:
python $SKILL
SCRIPTS/updatefile.py --file-id FILE
ID --src ./updatedreport.pdf
重命名 + 替换内容:
python $SKILL
SCRIPTS/updatefile.py --file-id FILE_ID --name v2.pdf --src ./v2.pdf
删除或移至回收站
bash
移至回收站(安全,可恢复):
python $SKILL
SCRIPTS/deletefile.py --file-id FILE_ID
永久删除(提示确认):
python $SKILL
SCRIPTS/deletefile.py --file-id FILE_ID --permanent
永久删除(非交互式):
python $SKILL
SCRIPTS/deletefile.py --file-id FILE_ID --permanent --yes
管理权限
bash
将文件设为公开可读(任何有链接的人):
python $SKILL
SCRIPTS/setpermissions.py --file-id FILE_ID --public
与用户共享为写入者:
python $SKILL
SCRIPTS/setpermissions.py --file-id FILE_ID --email user@example.com --role writer
与用户共享为读取者:
python $SKILL
SCRIPTS/setpermissions.py --file-id FILE_ID --email user@example.com --role reader
列出当前权限(返回 JSON):
python $SKILL
SCRIPTS/setpermissions.py --file-id FILE_ID --list
移除指定权限:
python $SKILL
SCRIPTS/setpermissions.py --file-id FILE
ID --remove PERMISSIONID
认证设置
选项 A — API 密钥(只读公开文件)
当文件/文件夹已共享为 任何有链接的人 且您只需要读取时使用。
python
from googleapiclient.discovery import build
APIKEY = os.environ[GOOGLEAPI_KEY]
drive = build(drive, v3, developerKey=API_KEY)
选项 B — 服务账号(对共享/公开驱动器的读写操作)
当您需要创建、修改或删除文件时使用。
python
from google.oauth2 import service_account
from googleapiclient.discovery import build
import os
SCOPES = [https://www.googleapis.com/auth/drive]
creds = serviceaccount.Credentials.fromserviceaccountfile(
os.environ[GOOGLESERVICEACCOUNT_JSON], scopes=SCOPES
)
drive = build(drive, v3, credentials=creds)
复制 assets/serviceaccounttemplate.json 查看预期结构。
将真实文件存储在仓库外部,切勿提交。
将 service_account.json 添加到 .gitignore。
文件和文件夹 ID
每个 Drive 资源都有一个唯一的 fileId。从共享链接中提取:
https://drive.google.com/drive/folders/FOLDERIDHERE
https://drive.google.com/file/d/FILEIDHERE/view
CRUD 操作
列出公开文件夹中的文件
python
def listfiles(drive, folderid: str) -> list[dict]:
results = []
page_token = None
while True:
resp = drive.files().list(
q=f{folder_id} in parents and trashed=false,
fields=nextPageToken, files(id, name, mimeType, size, modifiedTime),
pageToken=page_token,
supportsAllDrives=True,
includeItemsFromAllDrives=True,
).execute()
results.extend(resp.get(files, []))
page_token = resp.get(nextPageToken)
if not page_token:
break
return results
下载公开文件
python
import io
from googleapiclient.http import MediaIoBaseDownload
def downloadfile(drive, fileid: str, dest_path: