# Google Drive 离线下载

通过 Rclone (opens new window) 挂载谷歌硬盘并将 Aria2 下载完的内容移动到已挂载的谷歌硬盘中。

注意

请确保服务器可以正常访问 Google Drive

实现方式

Aria2 将文件下载到服务器,然后通过脚本自动将文件移动或复制到谷歌硬盘。因此文件大小不能超出服务器实际可用硬盘空间。 为什么不直接将文件下载到谷歌硬盘?因为不能!

Last updated on 2019-05-07


  • 本文在 Ubuntu 18.04 环境下操作

# 挂载谷歌硬盘

  • 安装 Rclone
apt -y install unzip curl fuse && curl https://rclone.org/install.sh | bash

脚本安装后可能出现错误提示,实测不影响使用;官方脚本也很奇葩

  • 开始配置

执行以下命令开始新建挂载

rclone config

显示如下,输入 n 并回车

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> 

这里输入名字后面会用到

name> googledrive

这里输入 1211 是谷歌云存储

11 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
12 / Google Drive

这里两步直接回车不输入内容

client_id>
client_secret>

这里输入 1 然后回车

Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1

这里两步直接回车不输入内容

root_folder_id> 
service_account_file>

这里输入 n 并回车

Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> 
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> n

这时显示让你打开一个网址

  • 用浏览器打开这个网址然后登录你想要挂载谷歌硬盘的谷歌账号
  • 这时会显示授权,选择允许
  • 将代码复制并粘贴即可
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.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=b8d8984d8ae30254163ae602d81dc735
Log in and authorize rclone for access
Enter verification code> 这里粘贴授权代码

这里问你是否是团队账号,一般选择否

Configure this as a team drive?
y) Yes
n) No
y/n> n

这一步输入 y

[googledrive]
type = drive
scope = drive
token = {"access_token":"ya29.GltfBn_YA8e0lgJNKeMAHZ3b_IeIBXHHrJxMO7wfW0AsY6v_Nso8YczhZafVe8UIIgK6ft1dn6BqP-UWp-W2YXBtcf6zbLuIZgKcPqwnhsVAkx3f7QcO5m0EUvAv","token_type":"Bearer","refresh_token":"1/hFSVnEeJ8CkFmrpdd7IGNx69mhkj8Cpny6JUktv3xcY","expiry":"2018-11-25T10:42:09.400295503Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

最后这里输入 q 即可完毕并退出

Name                 Type
====                 ====
googledrive          drive

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
  • 挂载到系统

创建一个文件夹用来挂载谷歌硬盘,文件夹位置和名称任意;挂载目录通常为 /mnt

mkdir /mnt/googledrive

输入以下命令挂载

rclone mount googledrive:/ /mnt/googledrive --allow-other --allow-non-empty --vfs-cache-mode writes &

mount 后面的 googledrive 是你开头输入的名字,:/ 中的 / 表示谷歌硬盘根目录,可以替换为想要挂载的目录

/mnt/googledrive 表示将谷歌硬盘挂载到刚才创建的文件夹中

输入 df -h 验证;可以看到谷歌硬盘

Filesystem      Size  Used Avail Use% Mounted on
udev            239M     0  239M   0% /dev
tmpfs            50M  2.9M   47M   6% /run
/dev/sda2       9.4G  1.9G  7.0G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           249M     0  249M   0% /sys/fs/cgroup
/dev/sda1       453M   42M  388M  10% /boot
googledrive:     15G  5.4G  9.7G  36% /mnt/googledrive 
tmpfs            50M     0   50M   0% /run/user/0
  • 开机自动挂载

点击这里 (opens new window) 下载 Rclone 启动脚本

更改这里然后保存即可

NAME="googledrive" #rclone name名
REMOTE='/' #远程文件夹
LOCAL='/mnt/googledrive' #挂载地址

放到 init.d 文件夹中

chmod +x rcloned && mv rcloned /etc/init.d/

编辑 rc.local 并添加以下内容然后保存

vi /etc/rc.local

bash /etc/init.d/rcloned start

到这里挂载谷歌硬盘所有的操作全部完成

# Aria2 配置

未安装 Aria2 可以点此查看安装教程;

上面安装教程中参考配置最后一行写了一句

#on-download-complete= //下载完成后执行脚本,这里填写脚本绝对路径

利用这一句将下载完毕的文件移动到谷歌硬盘

创建一个脚本

vi rcloneupload.sh

填写以下内容,注意更改 RemoteDIRLocalDIR 为实际的路径

#!/bin/bash

GID="$1";
FileNum="$2";
File="$3";
MinSize="5"  #限制最低上传大小,默认5k
MaxSize="157286400"  #限制最高文件大小(单位k),默认15G
RemoteDIR="/data/GoogleDrive/";  #rclone挂载的本地文件夹,最后面保留 /
LocalDIR="/data/Download/";  #Aria2下载目录,最后面保留 /

if [[ -z $(echo "$FileNum" |grep -o '[0-9]*' |head -n1) ]]; then FileNum='0'; fi
if [[ "$FileNum" -le '0' ]]; then exit 0; fi
if [[ "$#" != '3' ]]; then exit 0; fi

function LoadFile(){
  IFS_BAK=$IFS
  IFS=$'\n'
  if [[ ! -d "$LocalDIR" ]]; then return; fi
  if [[ -e "$File" ]]; then
    FileLoad="${File/#$LocalDIR}"
    while true
      do
        if [[ "$FileLoad" == '/' ]]; then return; fi
        echo "$FileLoad" |grep -q '/';
        if [[ "$?" == "0" ]]; then
          FileLoad=$(dirname "$FileLoad");
        else
          break;
        fi;
      done;
    if [[ "$FileLoad" == "$LocalDIR" ]]; then return; fi
    EXEC="$(command -v mv)"
    if [[ -z "$EXEC" ]]; then return; fi
    Option=" -f";
    cd "$LocalDIR";
    if [[ -e "$FileLoad" ]]; then
      ItemSize=$(du -s "$FileLoad" |cut -f1 |grep -o '[0-9]*' |head -n1)
      if [[ -z "$ItemSize" ]]; then return; fi
      if [[ "$ItemSize" -le "$MinSize" ]]; then
        echo -ne "\033[33m$FileLoad \033[0mtoo small to spik.\n";
        return;
      fi
      if [[ "$ItemSize" -ge "$MaxSize" ]]; then
        echo -ne "\033[33m$FileLoad \033[0mtoo large to spik.\n";
        return;
      fi
      eval "${EXEC}${Option}" \'"${FileLoad}"\' "${RemoteDIR}";
      if [[ $? == '0' ]]; then
        rm -rf "$FileLoad";
      fi
    fi
  fi
  IFS=$IFS_BAK
}
LoadFile;

添加权限

chmod +x rcloneupload.sh

编辑 Aria2 配置文件,添加配置和脚本路径

on-download-complete=/root/rcloneupload.sh

重启 Aria2 即可

至此,下载完毕的文件会自动移动到谷歌硬盘