利用 Aria2 和 Rclone 实现 OneDrive 离线下载(伪)
Aria2 是老毛子主导开发并开源的,基于 C 语言的轻量高性能跨平台多线程下载软件,支持 HTTP
和 磁力链接
下载。Rclone 同样是一款高性能跨平台开源程序,它基于 Go 语言开发,支持对接和操作几十种主流网盘和对象存储产品,包括 Aliyun OSS
、OneDrive
、Google Drive
等。
本文主要介绍如何在 Debian 9+/Ubuntu 18+
和 CentOS 7+
上安装 Rclone
,对接 OneDrive
,以及利用 Aria2
的 Event Hook 实现文件下载后自动上传到 OneDrive
网盘。不难看出,使用这种方式,上传和下载都要经 VPS,不仅速度慢,还将产生可观的流量,并非传统意义上的 离线下载
。
获取 OneDrive Token
下文你将看到,因为 VPS 不方便使用图形界面浏览器,所以这一步我们选择在本地 Windows 下完成。
下载并解压 rclone-current-windows-amd64.zip,随后进入解压后的最内层文件夹(包含 rclone.exe)。此时,按住键盘 Shift
键的同时,右键文件夹空白处,选择 在此处打开 Powershell 窗口(S)
(CMD 也可以),随后复制粘贴以下命令并回车,打开 Rclone 配置菜单
。
.\rclone.exe config
接着按照下面的示例和中文注释选择菜单的每一项,生成 OneDrive Token。
[collapse title="Config Menu"]
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n # 输入 n 回车,新建远端配置
name> od # 输入 od 回车,为远端配置命名(为保持下文一致性,不要使用其他名称)
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 Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Dropbox
\ "dropbox"
9 / Encrypt/Decrypt a remote
\ "crypt"
10 / FTP Connection
\ "ftp"
11 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
12 / Google Drive
\ "drive"
13 / Google Photos
\ "google photos"
14 / Hubic
\ "hubic"
15 / JottaCloud
\ "jottacloud"
16 / Koofr
\ "koofr"
17 / Local Disk
\ "local"
18 / Mega
\ "mega"
19 / Microsoft Azure Blob Storage
\ "azureblob"
20 / Microsoft OneDrive
\ "onedrive"
21 / OpenDrive
\ "opendrive"
22 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
23 / Pcloud
\ "pcloud"
24 / Put.io
\ "putio"
25 / QingCloud Object Storage
\ "qingstor"
26 / SSH/SFTP Connection
\ "sftp"
27 / Union merges the contents of several remotes
\ "union"
28 / Webdav
\ "webdav"
29 / Yandex Disk
\ "yandex"
30 / http Connection
\ "http"
31 / premiumize.me
\ "premiumizeme"
Storage> 20 # 输入 20 回车,选择 Microsoft OneDrive,如果该项不是 20,则根据实际数字选择
** See help for onedrive backend at: https://rclone.org/onedrive/ **
Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> # 直接回车
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 直接回车
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n # 输入 n 回车,不使用高级配置
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> y # 输入 y 回车,使用自动配置。此时浏览器会自动打开,请在打开的页面中登录 OneDrive 以获取 Token
If your browser doesnt open automatically go to the following link: http://127.0.0.1:53682/auth?state=e075eabcf52d5094a837f1b3e12977de
Log in and authorize rclone for access
Waiting for code...
Got code
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
3 / Type in driveID
\ "driveid"
4 / Type in SiteID
\ "siteid"
5 / Search a Sharepoint site
\ "search"
Your choice> 1 # 输入 1 回车
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!ZyWPB3G6-EetswlYu40wymfxwt_DMENCvHVsnO89SKEvQjaMk22FTYhImrbqkbkN
Chose drive to use:> 0 # 输入 0 回车
Found drive 'root' of type 'business', URL: https://onedroptbsteduau-my.sharepoint.com/personal/102419_onmicrosoft_net/Documents
Is that okay?
y) Yes
n) No
y/n> y # 输入 y 回车
2019/10/11 17:44:44 ERROR : Failed saving config "drive_id" = "b!ZyWPB3G6-EetswlYu40wymfxwt_DMENCvHVsnO89SKEvQjaMk22FTYhImrbqkbkN" in section "od" of the config file: section 'od' not found
2019/10/11 17:44:44 ERROR : Failed saving config "drive_type" = "business" in section "od" of the config file: section 'od' not found
--------------------
[od]
type = onedrive
token = {"access_token":"xxx","token_type":"Bearer","refresh_token":"xxx","expiry":"2019-10-11T18:44:28.775745+08:00"}
drive_id = b!ZyWPB3G6-EetswlYu40wymfxwt_DMENCvHVsnO89SKEvQjaMk22FTYhImrbqkbkN
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y # 输入 y 回车
Current remotes:
Name Type
==== ====
od 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> q # 输入 q 回车,退出配置
[/collapse]
完成上述步骤后,你将看到 Powershell 窗口出现类似下面的代码片段,其中 xxx
很长,已被删去。不要关闭窗口,待会要将它复制到 VPS 中(上下的分割线不复制)。
--------------------
[od]
type = onedrive
token = {"access_token":"xxx","token_type":"Bearer","refresh_token":"xxx","expiry":"2019-10-11T18:44:28.775745+08:00"}
drive_id = b!ZyWPB3G6-EetswlYu40wymfxwt_DMENCvHVsnO89SKEvQjaMk22FTYhImrbqkbkN
drive_type = business
--------------------
安装 Rclone
x86
架构可直接 复制
下面的安装命令 粘贴
到 SSH 窗口中 回车
,ARM
等其他架构的安装包不妨到 官网 下载。
Debian 9+/Ubuntu 18+ x64
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.deb && \
dpkg -i rclone-current-linux-amd64.deb
CentOS 7+ x64
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.rpm && \
rpm -i rclone-current-linux-amd64.rpm
导入 OneDrive 配置
复制以下命令粘贴到 SSH 窗口中回车。
CONF_DIR=/root/.config/rclone
[[ ! -d "${CONF_DIR}" ]] && mkdir -p "${CONF_DIR}"
cat > "${CONF_DIR}/rclone.conf"
随后 复制
第一步得到的包含 OneDrive Token 的 配置内容
(Powershell 复制方式为:选中内容后回车。上下分割线不要复制),粘贴
到 SSH 窗口中 回车
,随后按 Ctrl + D
保存。
测试 OneDrive 是否对接成功
复制粘贴以下命令回车,观察能否获取 OneDrive 文件列表。
rclone ls od:/
复制粘贴以下命令回车,观察能否上传 testfile
文件到网盘根目录。
touch testfile
rclone copy testfile od:/
为 Aria2 添加自动上传脚本
使用 Visual Studio Code 等高级文本编辑器,修改下面脚本中的 LOCAL_DIR
,即 Aria2 下载路径;REMOTE_DIR
,即 OneDrive 上传路径后,复制粘贴到 SSH 窗口中回车。
[collapse title="Shell Code"]
SAVE_DIR=/usr/local/etc/aria2-rclone
[[ ! -d "${SAVE_DIR}" ]] && mkdir -p "${SAVE_DIR}"
touch "${SAVE_DIR}/upload.sh"
chmod +x "${SAVE_DIR}/upload.sh"
cat > "${SAVE_DIR}/upload.sh" <<\EOF
#!/usr/bin/env bash
GID="$1"
FILE_NUM="$2"
FILE="$3"
##############################
LOCAL_DIR=/mnt/aria2/downloads
REMOTE_DIR=od:/Movies
##############################
LOG_FILE=/usr/local/etc/aria2-rclone/upload.log
function analyse_directory() {
directory="${FILE}"
parent_directory="$(dirname "${directory}")"
while [[ "${parent_directory}" != "${LOCAL_DIR}" ]]; do
has_sub_directory=1
directory="${parent_directory}"
parent_directory="$(dirname "${parent_directory}")"
done
if [[ "${has_sub_directory}" ]]; then
FILE="${directory}"
REMOTE_DIR="${REMOTE_DIR}${directory#$parent_directory}"
fi
}
[[ ! "${FILE_NUM}" ]] && exit
[[ "${FILE_NUM}" -eq 0 ]] && exit
analyse_directory
cat >> "${LOG_FILE}" <<ENDOFFILE
=========================================================================
Local: $FILE
Remote: ${REMOTE_DIR}
ENDOFFILE
rclone copy "${FILE}" "${REMOTE_DIR}" --log-file "${LOG_FILE}" --stats=5m --stats-one-line --log-level INFO
# 如需上传后删除文件,请删掉下一行开头的 '#'
# rm -rf "${FILE}"
EOF
[/collapse]
随后,修改 Aria2 配置文件 aria2.conf
,在其中添加下面这行即可。
on-download-complete=/usr/local/etc/aria2-rclone/upload.sh
最后,重启 Aria2,添加任务,观察下载完毕后能否上传到 OneDrive,上传日志可通过以下命令查看。
cat /usr/local/etc/aria2-rclone/upload.log
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »