0%

一个好用的GoogleDriver命令行工具

工作上经常会进行一些数据文件的导出分享,手动的工作不胜其烦,因此希望通过命令行实现自动化处理。考虑过使用ftp服务,但是不被公司安全策略允许,也考虑过使用邮件自动发送,然而目前主流邮箱对于附件的大小限制基本无法满足需要。

Google产品一直被视为开放的典范,尤其对于工程师来说,开放了为数众多的授权API接口,用于内容操作,因此萌生了通过Google Drive进行文件分享的念头。

在进行尝试的过程中,发现了一个命令行的Google Drive工具,看来与我有相同困扰的人不少。😂

gdrive安装

  • Mac 可通过brew直接安装
    1
    $ brew install gdrive;
  • 对于linux,可通过以下步骤安装
    1
    2
    3
    4
    $ wget https://docs.google.com/uc?id=0B3X9GlR6EmbnWksyTEtCM0VfaFE&export=download
    $ mv uc\?id\=0B3X9GlR6EmbnWksyTEtCM0VfaFE gdrive
    $ chmod +x gdrive
    $ sudo install gdrive /usr/local/bin/gdrive

gdrive配置

初次使用gdrive命令,会被引导进行授权配置,使用的是Oauth协议

1
2
3
4
5
6
$ gdrive list
Authentication needed
Go to the following url in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state

Enter verification code:

即是要求使用者账户给予gdrive授权,在浏览器中打开上述连接,根据引导获得授权auth_token,复制到命令行界面。

完成此操作后可在用户Home下发现.gdrive目录,其中token_v2.json文件保存了用户的auth_token信息,这是一个Oauth2的授权。

1
2
3
drwx------  4.0K Jun 15 15:40 ./
drwxr-xr-x 4.0K Jun 15 15:40 ../
-rw------- 298 Jun 15 15:29 token_v2.json

默认情况下gdrive命令会使用用户目录下的.gdrive授权,同时可以在命令中通过以下参数提供授权凭据,以下全文通过[global]标识:

1
2
3
4
-c, --config <configDir>         Application path, default: /Users/<user>/.gdrive
--refresh-token <refreshToken> Oauth refresh token used to get access token (for advanced users)
--access-token <accessToken> Oauth access token, only recommended for short-lived requests because of short lifetime (for advanced users)
--service-account <accountFile> Oauth service account filename, used for server to server communication without user interaction (file is relative to config dir)

gdrive常用命令

目录列举

1
2
3
4
5
6
7
8
9
10
gdrive [global] list [options]

options:
-m, --max <maxFiles> 最大显示文件数: 30
-q, --query <query> 过滤命令,默认为: "trashed = false and 'me' in owners". 查看https://developers.google.com/drive/search-parameters
--order <sortOrder> 排序. 查看See https://godoc.org/google.golang.org/api/drive/v3#FilesListCall.OrderBy
--name-width <nameWidth> 文件显示字符宽度, 默认: 40, 最小: 9, 可使用 0 表示显示全部 full width
--absolute Show absolute path to file (will only show path from first parent)
--no-header Dont print the header
--bytes Size in bytes

文件或目录下载

1
2
3
4
5
6
7
gdrive [global] download query [options] <query>

options:
-f, --force 覆盖本地同名文件
-r, --recursive 下载目录及其子目录,documents will be skipped
--path <path> 下载路径
--no-progress 隐藏下载进度

文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
gdrive [global] upload [options] <path>

options:
-r, --recursive 上传目录及其子目录
-p, --parent <parent> 父目录id,可以指定多个父目录
--name <name> 保存文件名
--description <description> 文件描述
--no-progress 隐藏上传进度
--mime <mime> 强制mime
--share 公开分享文件
--delete 上传完成后删除本地文件
--timeout <timeout> 超时时间,0为永不超时,默认为300
--chunksize <chunksize> 上传分片大小,默认为: 8388608

通过标准输入上传文件

1
2
3
4
5
6
7
8
9
10
gdrive [global] upload - [options] <name>

options:
-p, --parent <parent> 父目录id,可以指定多个父目录
--chunksize <chunksize> 上传分片大小,默认为: 8388608
--description <description> 文件描述
--mime <mime> 强制mime
--share 公开分享文件
--timeout <timeout> 超时时间,0为永不超时,默认为300
--no-progress 隐藏上传进度

更新文件,将产生文件的一个新版本

1
2
3
4
5
6
7
8
9
10
gdrive [global] update [options] <fileId> <path>
options:
-p, --parent <parent> 父目录id,可以指定多个父目录
--chunksize <chunksize> 上传分片大小,默认为: 8388608
--description <description> 文件描述
--mime <mime> 强制mime
--share 公开分享文件
--timeout <timeout> 超时时间,0为永不超时,默认为300
--no-progress 隐藏上传进度
// fileId可通过上传时的响应获得

分享文件或目录

1
2
3
4
5
6
7
8
gdrive [global] share [options] <fileId>

options:
--role <role> 权限: owner/writer/commenter/reader, 默认: reader
--type <type> 分享范围: user/group/domain/anyone, 默认: anyone
--email <email> 分享给指定组或用户,需要设置type为'user'或 'group'
--discoverable 允许搜索引擎搜索
--revoke 回收其他权限(owner权限不回收)

删除文件

1
2
3
4
gdrive [global] delete [options] <fileId>

options:
-r, --recursive Delete directory and all it's content

同步本地文件夹到drive

1
2
3
4
5
6
7
8
9
10
11
gdrive [global] sync upload [options] <path> <fileId>

options:
--keep-remote 发生冲突时保留服务端文件
--keep-local 发生冲突时保留本地文件
--keep-largest 发生冲突时保留最大文件
--delete-extraneous 删除服务端其他文件
--dry-run Show what would have been transferred
--no-progress Hide progress
--timeout <timeout> 超时时间,0为永不超时,默认为300
--chunksize <chunksize> 上传分片大小,默认为: 8388608

下载指定版本文件

1
2
3
4
5
6
7
8
gdrive [global] revision download [options] <fileId> <revId>

options:
-f, --force 覆盖本地文件
--no-progress 隐藏进度条
--stdout 输出到标准输出
--path <path> 下载路径
--timeout <timeout> Set timeout in seconds, use 0 for no timeout. Timeout is reached when no data is transferred in set amount of seconds, default: 300

删除指定文件指定版本

1
gdrive [global] revision delete <fileId> <revId>

一些使用🌰

背景描述:需要定时从数据库导出数据给需求方,从安全性角度考虑,不建议从线下环境直接操作数据库连接读取数据,因此只能将数据导出到线上服务器数据文件中,但线上数据文件分享给用户往往需要经过多次文件复制。这里的策略是将数据上传到公司账户Google Drive,然后分享给指定用户或组。

这里数据文件保存在/tmp/data.xlsx中,数据需求方为pm@shopee.com,测试人员为qa@shopee.com。公司账户已通过上述方式配置

操作过程如下,请注意命令的输入及输出

1
2
3
4
5
6
7
8
9
10
11
12
$ gdrive mkdir exports
Directory 1IXMJ5_TBjjN_2m-rd_HaLeIX3Ch7BLOE created

$ gdrive upload -p 1IXMJ5_TBjjN_2m-rd_HaLeIX3Ch7BLOE --name data.xlsx /tmp/data.xlsx
Uploading data.xlsx
Uploaded 1a-bGQqHQt8XIOaEemAQQJCiRTEFe75kK at 12.7 MB/s, total 31.5 MB

$ gdrive share --type user --email pm@shopee.com,qa@shopee.com

// crontab 任务
$ crontab -l
0 3 * * * cd ~/auto; /* 数据导出命令 */ && gdrive update 1a-bGQqHQt8XIOaEemAQQJCiRTEFe75kK /tmp/data.xlsx; gdrive share --type user --email pm@shopee.com 1a-bGQqHQt8XIOaEemAQQJCiRTEFe75kK; gdrive share --type user --email qa@shopee.com 1a-bGQqHQt8XIOaEemAQQJCiRTEFe75kK;

Done!