由于百度网盘、115网盘等的普及,通常我们在使用网盘的时候,都是按照了这些软件的习惯,使用特定客户端,有些时候并不是很方便。

其实有些网盘,本身是支持API的,在API的基础上,我们就可以自行开发出第三方的网盘客户端了。

Rclone简介

Rclone是一款可以直接将网盘挂载到本地目录的程序,我们可以像时候本地的磁盘一样使用挂载好的网盘。
今天记录一下在linux环境下,OneDrive的挂载过程。

下载Rclone

github

1
https://github.com/rclone/rclone/releases

官网

1
https://rclone.org/downloads/

为了更方便安装,我们需要下载Windows版本,用于获取网盘的access_token。目前最新版为【rclone-v1.57.0-windows-amd64】
Linux版本可以不用下载,直接使用一键安装脚本即可。

获取access_token

下载好安装包后先进行解压。
如我解压后的路径为:

1
D:\ashin\Downloads\rclone-v1.57.0-windows-amd64\rclone-v1.57.0-windows-amd64

打开windows的命令行窗口,运行如下两条命令:

1
cd D:\ashin\Downloads\rclone-v1.57.0-windows-amd64\rclone-v1.57.0-windows-amd64

++注意将路径更改为自己的rclone文件所在路径。++

1
.\rclone.exe authorize "onedrive"

运行后浏览器将会打开OneDrive登陆页面,根据提示登陆自己的OneDrive账号,等待页面提示【Success!】

再次回到命令行窗口,此时会出现我们的access_token。

根据提示,复制整段内容(我图中标记阴影部分)。

Linux安装Rclone

使用root用户ssh登录到linux客户端,执行以下命令自动安装最新版。

1
curl https://rclone.org/install.sh | sudo bash

等待提示成功【rclone v1.57.0 has successfully installed.】。

配置OneDrive

运行rclone config

1
rclone config

输出:

1
2
3
4
5
6
7
root@instance-20220204-2125:~# rclone config
<5>NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q>

这里输出【n】,表示新建。并且根据提示输入一个自定义的名称以及挂载的类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
n/s/q> n
name> onedrive-mac
Option Storage.
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value.
1 / 1Fichier
\ "fichier"
2 / Alias for an existing remote
\ "alias"
3 / Amazon Drive
\ "amazon cloud drive"
4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Better checksums for other remotes
\ "hasher"
7 / Box
\ "box"
8 / Cache a remote
\ "cache"
9 / Citrix Sharefile
\ "sharefile"
10 / Compress a remote
\ "compress"
11 / Dropbox
\ "dropbox"
12 / Encrypt/Decrypt a remote
\ "crypt"
13 / Enterprise File Fabric
\ "filefabric"
14 / FTP Connection
\ "ftp"
15 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
16 / Google Drive
\ "drive"
17 / Google Photos
\ "google photos"
18 / Hadoop distributed file system
\ "hdfs"
19 / Hubic
\ "hubic"
20 / In memory object storage system.
\ "memory"
21 / Jottacloud
\ "jottacloud"
22 / Koofr
\ "koofr"
23 / Local Disk
\ "local"
24 / Mail.ru Cloud
\ "mailru"
25 / Mega
\ "mega"
26 / Microsoft Azure Blob Storage
\ "azureblob"
27 / Microsoft OneDrive
\ "onedrive"
28 / OpenDrive
\ "opendrive"
29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
30 / Pcloud
\ "pcloud"
31 / Put.io
\ "putio"
32 / QingCloud Object Storage
\ "qingstor"
33 / SSH/SFTP Connection
\ "sftp"
34 / Sia Decentralized Cloud
\ "sia"
35 / Sugarsync
\ "sugarsync"
36 / Tardigrade Decentralized Cloud Storage
\ "tardigrade"
37 / Transparently chunk/split large files
\ "chunker"
38 / Union merges the contents of several upstream fs
\ "union"
39 / Uptobox
\ "uptobox"
40 / Webdav
\ "webdav"
41 / Yandex Disk
\ "yandex"
42 / Zoho
\ "zoho"
43 / http Connection
\ "http"
44 / premiumize.me
\ "premiumizeme"
45 / seafile
\ "seafile"

Microsoft OneDrive编号为27,则输入27。
【client_id】和【client_secret】不了解的话直接默认即可。
服务提供商如果是21世纪互联选择【Azure and Office 365 operated by 21Vianet in China】,不清楚的话直接默认。
【Edit advanced config?】默认y
【Use auto config?】选择N,不自动配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Storage> 27
Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
Option region.
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value.
1 / Microsoft Cloud Global
\ "global"
2 / Microsoft Cloud for US Government
\ "us"
3 / Microsoft Cloud Germany
\ "de"
4 / Azure and Office 365 operated by 21Vianet in China
\ "cn"
region>
Edit advanced config?
y) Yes
n) No (default)
y/n>
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n> n
Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "onedrive"
Then paste the result.
Enter a string value. Press Enter for the default ("").

而后输入刚才在windows下获取到的access_token。其他选项参考图中所示。

提示添加成功后选择【y】,并且退出即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d>
Current remotes:

Name Type
==== ====
mac onedrive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>
e/n/d/r/c/s/q> q

安装fuse

挂载到磁盘我们还需要用到fuse这个工具。
Debian和Ubuntu:

1
apt install -y fuse

CentOS

1
yum install -y fuse

挂载OneDrive

建立挂载文件夹

选一个自己喜欢的目录

1
mkdir /onedrive

临时挂载

1
rclone mount mac:/ /onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

请注意,上面命令中:
1、【mac】为配置OneDrive步骤中自己填写的名称,我的为【mac】,需改成自己的。
2、/onedrive 为需要挂载到的本地目录,我的为/onedrive,需改为自己的。

执行后页面没有动静,为正常现象。

1
2
root@instance-20220204-2125:~# rclone mount mac:/ /onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
<5>NOTICE: One drive root '': --vfs-cache-mode writes or full is recommended for this remote as it can't stream

重新打开一个SSH窗口,确认是否成功挂载。

1
df -h

配置开机自动挂载

运行以下命令

1
command=" mount mac:/ /onedrive  --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000"

此命令后面为上面临时挂载命令去掉开头的rclone。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

[Service]
Type=simple
ExecStart=$(command -v rclone) ${command}
Restart=on-abort
User=root

[Install]
WantedBy=default.target
EOF

此为一条完整的shell命令,需要全部复制,一次执行。
启动

1
systemctl start rclone

开机自启

1
systemctl enable rclone

查看状态

1
systemctl status rclone

重启

1
systemctl restart  rclone

停止

1
systemctl status rclone

这样就挂载到linux里面了

Rclone常用功能选项

命令格式

1
2
3
4
5
6
7
8
# 本地到网盘
rclone [功能选项] <本地路径> <网盘名称:路径> [参数] [参数] ...

# 网盘到本地
rclone [功能选项] <网盘名称:路径> <本地路径> [参数] [参数] ...

# 网盘到网盘
rclone [功能选项] <网盘名称:路径> <网盘名称:路径> [参数] [参数] ...

举例

同步内容到网盘

1
/usr/bin/rclone sync -v /root/本地目录/ mac:/ > ~/drive.log 2>&1

同步内容到本地

1
/usr/bin/rclone sync -v mac:/ /root/本地目录/ > ~/drive.log 2>&1

其他命令

  • rclone copy - 复制
  • rclone move - 移动,如果要在移动后删除空源目录,请加上 –delete-empty-src-dirs 参数
  • rclone sync - 同步:将源目录同步到目标目录,只更改目标目录。
  • rclone size - 查看网盘文件占用大小。
  • rclone delete - 删除路径下的文件内容。
  • rclone purge - 删除路径及其所有文件内容。
  • rclone mkdir - 创建目录。
  • rclone rmdir - 删除目录。
  • rclone rmdirs - 删除指定灵境下的空目录。如果加上 –leave-root 参数,则不会删除根目录。
  • rclone check - 检查源和目的地址数据是否匹配。
  • rclone ls - 列出指定路径下的所有的文件以及文件大小和路径。
  • rclone lsl - 比上面多一个显示上传时间。
  • rclone lsd 列出指定路径下的目录
  • rclone lsf - 列出指定路径下的目录和文件