Linux使用Rclone挂载Google Drive网盘

相信不止是我,很多人为了省钱在选服务器时选择的是基础配置,存储容量只有可怜的几十GB甚至只有10GB。但是有时候又确实需要一个较大的存储空间,这时候提供了API的OneDrive、Google Drive等网盘,可以通过挂载到服务器作为本地存储使用,从而一定程度上帮助我们缓解容量焦虑。


步骤

1. 下载安装

本文默认使用的Linux系统,但是操作其实都差不多,其他系统也可以借鉴。

运行以下命令,下载并安装Rclone

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

2. 配置Rclone

  • 安装完成后输入:

    1
    
    rclone config
    
  • 输出如下:

    1
    2
    3
    4
    5
    
    No remotes found - make a new one
    n) New remote
    s) Set configuration password
    q) Quit config
    n/s/q>
    

    输入n新建配置,名字随意,此处我输入的是:

    1
    
    name> GDrive
    
  • 然后会要求选择挂载的网盘服务类型:

     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
    
    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"
    Storage> 16
    

    此处Google Drive序号为16,故输入16

  • 然后会要求输入OAuth client ID,包含client_idclient_secret,可前往我的另一篇文章《创建谷歌OAuth Client ID》了解获取方法:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    Option client_id.
    Google Application Client Id
    Setting your own is recommended.
    See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
    If you leave this blank, it will use an internal key which is low performance.
    Enter a string value. Press Enter for the default ("").
    client_id> 填入ID
    Option client_secret.
    OAuth Client Secret.
    Leave blank normally.
    Enter a string value. Press Enter for the default ("").
    client_secret> 填入密钥
    
  • 然后要选择Rclone对Google Drive网盘文件的操作权限:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    Option scope.
    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
    

    选择完全访问所有文件,不包括应用程序数据文件夹。即1

  • 接下来的选项大多默认即可,参照以下内容:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    Option root_folder_id.
    ID of the root folder.
    Leave blank normally.
    Fill in to access "Computers" folders (see docs), or for rclone to use
    a non root folder as its starting point.
    Enter a string value. Press Enter for the default ("").
    root_folder_id>
    Option service_account_file.
    Service Account Credentials JSON file path.
    Leave blank normally.
    Needed only if you want use SA instead of interactive login.
    Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.
    Enter a string value. Press Enter for the default ("").
    service_account_file>
    Edit advanced config?
    y) Yes
    n) No (default)
    y/n>
    
  • 注意此步需要登录,故选n

    1
    2
    3
    4
    5
    6
    7
    
    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
    
  • 复制链接到浏览器登录,登陆后会给config_verification_code,黏贴:

    1
    2
    3
    4
    5
    6
    
    Option config_verification_code.
    Verification code
    Go to this URL, authenticate then paste the code here.
    此处为链接,复制后到浏览器完成登录步骤,会给一个“config_verification_code”。
    Enter a string value. Press Enter for the default ("").
    config_verification_code> 在此填入“config_verification_code”。
    
  • 选择团队盘或者个人盘,此处我用的团队盘,故选y

    1
    2
    3
    4
    5
    
    Configure this as a Shared Drive (Team Drive)?
    
    y) Yes
    n) No (default)
    y/n> y
    
  • 至此配置结束,会输出所有的网盘配置信息进行确认,确认无误后按回车默认同意,接着输入q退出。然后使用此命令检测是否配置成功,若成功会输出网盘根目录下的目录信息:

    1
    
    rclone lsd <配置的网盘名称>:/
    

3. 挂载Google Drive网盘

根据需求不同,可参考下表自行修改,其他配置参数可前往Rclone官网查阅:

参数 说明
–allow-other 允许非当前rclone用户外其它用户进行访问
–attr-timeout 5m 文件属性缓存,(大小,修改时间等)的时间。如果VPS
–vfs-cache-mode full 开启VFS文件缓存,可减少rclone与API交互,同时可提高文件读写效率
–vfs-cache-max-age 24h VFS文件缓存时间,这里设置24小时,如果文件很少更改,建议设置更长时间
–vfs-cache-max-size 10G VFS文件缓存上限大小,请根据服务器剩余磁盘自行调节
–vfs-read-chunk-size-limit 100M 分块读取大小,这里设置的是100M,可提高文件读的效率,比如1G的文件,大致分为10个块进行读取,但与此同时API请求次数也会增多
–buffer-size 32M 该参数为读取每个文件时的内存缓冲区大小,控制rclone上传和挂载的时候的内存占用,调低点可以防止内存占用过高而崩溃,但太低可能会影响部分文件的传输速度,请根据服务器内存大小自行设置
–low-level-retries 该参数为传输文件没速度的时候重试次数,没速度的时候,单个会自动睡眠10ms起,然后再重试,不行,再睡眠更长一段时间,再重试,这样可以稍微加快文件上传进度
–transfers 该参数控制最大同时传输任务数量,如果你cpu性能差,建议调低,但太低可能会影响多个文件同时传输的速度
–daemon 后台运行程序

这里给出一个模板:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
rclone mount <配置的云盘名称>:<要挂载的云盘目录> <作为挂载点的本地目录> \
  --umask 0000 \
  --default-permissions \
  --allow-non-empty \
  --allow-other \
  --attr-timeout 5m \
  --transfers 4 \
  --buffer-size 32M \
  --low-level-retries 200 \
  --vfs-read-chunk-size 32M \
  --vfs-read-chunk-size-limit 128M \
  --vfs-cache-mode full \
  --vfs-cache-max-age 24h \
  --vfs-cache-max-size 10G \
  --daemon

如果无法挂载,请使用如下指令再试:

1
apt install fuse

挂载后使用以下命令测试,看到网盘目录即为成功:

1
df -h

如果需要取消挂载,使用如下命令:

1
fusermount -qzu <挂载路径>