返回顶部
g

google-drive-skill谷歌云端技能

|

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 0.1.0
安全检测
已通过
157
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

google-drive-skill

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 GOOGLEAPIKEY=yourapikey_here

读写(服务账号):

export GOOGLESERVICEACCOUNTJSON=/path/to/serviceaccount.json

参见 assets/serviceaccounttemplate.json 了解预期的 JSON 结构。

3. 解析 SKILL_SCRIPTS(参见本文件顶部),然后运行脚本


脚本命令

列出文件夹中的文件

bash

表格输出(默认):


python $SKILLSCRIPTS/listfiles.py --folder-id FOLDER_ID

JSON 输出:

python $SKILLSCRIPTS/listfiles.py --folder-id FOLDER_ID --json

下载文件

bash

保存到指定路径:


python $SKILLSCRIPTS/downloadfile.py --file-id FILE_ID --dest ./downloads/report.pdf

保存到目录(从 Drive 自动获取文件名):

python $SKILLSCRIPTS/downloadfile.py --file-id FILE_ID --dest ./downloads/

创建文件夹

bash

在 Drive 根目录下:


python $SKILLSCRIPTS/createfolder.py --name 我的新文件夹

在指定父文件夹内:

python $SKILLSCRIPTS/createfolder.py --name 子文件夹 --parent-id PARENTFOLDERID

上传文件

bash

基本上传(MIME 类型自动检测):


python $SKILLSCRIPTS/uploadfile.py --src ./report.pdf --parent-id FOLDER_ID

在 Drive 上自定义名称:

python $SKILLSCRIPTS/uploadfile.py --src ./report.pdf --name Q1-Report.pdf --parent-id FOLDER_ID

上传并立即设为公开可读:

python $SKILLSCRIPTS/uploadfile.py --src ./photo.png --parent-id FOLDER_ID --make-public

更新文件

bash

重命名:


python $SKILLSCRIPTS/updatefile.py --file-id FILE_ID --name 新名称.pdf

移动到不同文件夹:

python $SKILLSCRIPTS/updatefile.py --file-id FILEID --move-to NEWPARENTID --old-parent OLDPARENT_ID

替换文件内容:

python $SKILLSCRIPTS/updatefile.py --file-id FILEID --src ./updatedreport.pdf

重命名 + 替换内容:

python $SKILLSCRIPTS/updatefile.py --file-id FILE_ID --name v2.pdf --src ./v2.pdf

删除或移至回收站

bash

移至回收站(安全,可恢复):


python $SKILLSCRIPTS/deletefile.py --file-id FILE_ID

永久删除(提示确认):

python $SKILLSCRIPTS/deletefile.py --file-id FILE_ID --permanent

永久删除(非交互式):

python $SKILLSCRIPTS/deletefile.py --file-id FILE_ID --permanent --yes

管理权限

bash

将文件设为公开可读(任何有链接的人):


python $SKILLSCRIPTS/setpermissions.py --file-id FILE_ID --public

与用户共享为写入者:

python $SKILLSCRIPTS/setpermissions.py --file-id FILE_ID --email user@example.com --role writer

与用户共享为读取者:

python $SKILLSCRIPTS/setpermissions.py --file-id FILE_ID --email user@example.com --role reader

列出当前权限(返回 JSON):

python $SKILLSCRIPTS/setpermissions.py --file-id FILE_ID --list

移除指定权限:

python $SKILLSCRIPTS/setpermissions.py --file-id FILEID --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:

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 google-drive-skill-1776119830 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 google-drive-skill-1776119830 技能

通过命令行安装

skillhub install google-drive-skill-1776119830

下载

⬇ 下载 google-drive-skill v0.1.0(免费)

文件大小: 19.16 KB | 发布时间: 2026-4-17 14:58

v0.1.0 最新 2026-4-17 14:58
google-drive-skill 0.1.0 — Initial release

- Provides ready-to-run scripts for listing, reading, creating, updating, and deleting files and folders in Google Drive via the Drive API v3.
- Supports both read-only access (using API key) and full CRUD operations (using service account).
- Includes robust folder navigation, file upload/download, setting permissions, handling MIME types, and error management.
- Features an organized script interface with detailed usage examples and credential setup instructions.
- Useful supporting materials: service account template, MIME type references, error code guides.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部