gdcli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Google Drive CLI

Google Drive CLI

Command-line interface for Google Drive operations.
用于Google Drive操作的命令行界面工具。

Installation

安装

bash
npm install -g @mariozechner/gdcli
bash
npm install -g @mariozechner/gdcli

Setup

配置

Google Cloud Console (one-time)

Google Cloud 控制台(一次性操作)

  1. Create a new project (or select existing)
  2. Enable the Google Drive API
  3. Set app name in OAuth branding
  4. Add test users (all Gmail addresses you want to use)
  5. Create OAuth client:
    • Click "Create Client"
    • Application type: "Desktop app"
    • Download the JSON file
  1. 创建新项目(或选择现有项目)
  2. 启用Google Drive API
  3. 在OAuth品牌设置中设置应用名称
  4. 添加测试用户(所有你想使用的Gmail地址)
  5. 创建OAuth客户端:
    • 点击“创建客户端”
    • 应用类型:“桌面应用”
    • 下载JSON文件

Configure gdcli

配置gdcli

First check if already configured:
bash
gdcli accounts list
If no accounts, guide the user through setup:
  1. Ask if they have a Google Cloud project with Drive API enabled
  2. If not, walk them through the Google Cloud Console steps above
  3. Have them download the OAuth credentials JSON
  4. Run:
    gdcli accounts credentials ~/path/to/credentials.json
  5. Run:
    gdcli accounts add <email>
    (use
    --manual
    for browserless OAuth)
首先检查是否已完成配置:
bash
gdcli accounts list
如果没有添加任何账户,按照以下步骤引导完成设置:
  1. 询问用户是否已拥有启用Drive API的Google Cloud项目
  2. 如果没有,引导用户完成上述Google Cloud控制台的操作步骤
  3. 让用户下载OAuth凭证JSON文件
  4. 运行:
    gdcli accounts credentials ~/path/to/credentials.json
  5. 运行:
    gdcli accounts add <email>
    (使用
    --manual
    参数进行无浏览器OAuth验证)

Usage

使用方法

Run
gdcli --help
for full command reference.
Common operations:
  • gdcli <email> ls [folderId]
    - List files/folders
  • gdcli <email> ls --query "<query>"
    - List with Drive query filter
  • gdcli <email> search "<text>"
    - Full-text content search
  • gdcli <email> download <fileId> [destPath]
    - Download a file
  • gdcli <email> upload <localPath> [--folder <folderId>]
    - Upload a file
  • gdcli <email> mkdir <name>
    - Create a folder
  • gdcli <email> share <fileId> --anyone
    - Share publicly
运行
gdcli --help
查看完整命令参考。
常见操作:
  • gdcli <email> ls [folderId]
    - 列出文件/文件夹
  • gdcli <email> ls --query "<query>"
    - 使用Drive查询过滤器列出内容
  • gdcli <email> search "<text>"
    - 全文内容搜索
  • gdcli <email> download <fileId> [destPath]
    - 下载文件
  • gdcli <email> upload <localPath> [--folder <folderId>]
    - 上传文件
  • gdcli <email> mkdir <name>
    - 创建文件夹
  • gdcli <email> share <fileId> --anyone
    - 公开共享

Search

搜索

Two different commands:
  • search "<text>"
    - Searches inside file contents (fullText)
  • ls --query "<query>"
    - Filters by metadata (name, type, date, etc.)
Use
ls --query
for filename searches!
两种不同的命令:
  • search "<text>"
    - 搜索文件内容(全文检索)
  • ls --query "<query>"
    - 按元数据筛选(名称、类型、日期等)
文件名搜索请使用
ls --query

Query Syntax (for ls --query)

查询语法(适用于ls --query)

Format:
field operator value
. Combine with
and
/
or
, group with
()
.
Operators:
=
,
!=
,
contains
,
<
,
>
,
<=
,
>=
Examples:
bash
undefined
格式:
字段 操作符 值
。使用
and
/
or
组合条件,用
()
分组。
操作符:
=
,
!=
,
contains
,
<
,
>
,
<=
,
>=
示例:
bash
undefined

By filename

按文件名

ls --query "name = 'report.pdf'" # exact match ls --query "name contains 'IMG'" # prefix match
ls --query "name = 'report.pdf'" # 精确匹配 ls --query "name contains 'IMG'" # 前缀匹配

By type

按类型

ls --query "mimeType = 'application/pdf'" ls --query "mimeType contains 'image/'" ls --query "mimeType = 'application/vnd.google-apps.folder'" # folders
ls --query "mimeType = 'application/pdf'" ls --query "mimeType contains 'image/'" ls --query "mimeType = 'application/vnd.google-apps.folder'" # 文件夹

By date

按日期

ls --query "modifiedTime > '2024-01-01'"
ls --query "modifiedTime > '2024-01-01'"

By owner/sharing

按所有者/共享状态

ls --query "'me' in owners" ls --query "sharedWithMe"
ls --query "'me' in owners" ls --query "sharedWithMe"

Exclude trash

排除已删除文件

ls --query "trashed = false"
ls --query "trashed = false"

Combined

组合条件

ls --query "name contains 'report' and mimeType = 'application/pdf'"

Ref: https://developers.google.com/drive/api/guides/ref-search-terms
ls --query "name contains 'report' and mimeType = 'application/pdf'"

参考文档:https://developers.google.com/drive/api/guides/ref-search-terms

Data Storage

数据存储

  • ~/.gdcli/credentials.json
    - OAuth client credentials
  • ~/.gdcli/accounts.json
    - Account tokens
  • ~/.gdcli/downloads/
    - Default download location
  • ~/.gdcli/credentials.json
    - OAuth客户端凭证
  • ~/.gdcli/accounts.json
    - 账户令牌
  • ~/.gdcli/downloads/
    - 默认下载路径