使用google-drive-ocamlfuse在SSH连接和CUI操作的Linux (Debian) 上挂载GoogleDrive

我今天要写一篇关于如何使用SSH连接、通过CUI操作的Linux系统来将GoogleDrive挂载到google-drive-ocamlfuse上的方法。

 

你为什么会想要做这件事呢?

最近,我在GCP的Spot虚拟机上经常需要将文件移动到GoogleDrive上。每次都要采取以下步骤:先通过SSH从虚拟机下载文件到本地PC,然后再上传到GoogleDrive。这个过程非常耗时。

出于简化操作的考虑,我决定将GoogleDrive直接挂载到VM上,以便能够直接从VM向GoogleDrive上传文件。

通过这个功能,可以将GoogleDrive的内容作为VM目录查看,并且可以使用cp命令和mv命令!

前提条件 (Qian ti tiao jian)

    • マシンスペック: Linux: Debian 11

 

    • GoogleCloudPlatformのGoogleDriveAPIを有効にしてあること

 

    VMとは別に、ブラウザが利用できる端末をもっていること

设定方式

安装google-drive-ocamlfuse

但我尝试过从官方文档中提到的通过PPA或DEB文件进行安装的方法,效果不佳。

然后,我参考了这个,并通过opam进行了安装。

sudo apt install opam -y
opam init # プロファイルの設定など不要なところはNを選択
opam install google-drive-ocamlfuse

安装完成后,将以下路径添加到~/.bashrc中。

PATH="$PATH:$HOME/.opam/default/bin"

只需在中文中提供一种选项:当你运行 “source ~/.bashrc” 之后,就可以使用 google-drive-ocamlfuse 命令了。

(失败的方法1)如果通过PPA安装google-drive-ocamlfuse

当我尝试输入公式指令时,

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:alessandro-strada/ppa
$ sudo apt update

发生了以下错误。

$ sudo apt update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian bullseye-updates InRelease
Hit:3 http://security.debian.org/debian-security bullseye-security InRelease
Hit:4 http://deb.debian.org/debian bullseye-backports InRelease
Hit:5 http://packages.cloud.google.com/apt cloud-sdk-bullseye InRelease
Hit:6 http://packages.cloud.google.com/apt google-cloud-packages-archive-keyring-bullseye InRelease
Hit:7 http://packages.cloud.google.com/apt google-compute-engine-bullseye-stable InRelease
Ign:8 http://ppa.launchpad.net/alessandro-strada/ppa/ubuntu mantic InRelease
Err:9 http://ppa.launchpad.net/alessandro-strada/ppa/ubuntu mantic Release
  404  Not Found [IP: 185.125.190.52 80]
Reading package lists... Done
E: The repository 'http://ppa.launchpad.net/alessandro-strada/ppa/ubuntu mantic Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

根据官方维基上的记载,这似乎是指自2020年起无法再使用了。

(第二种失败的方法)如果从DEB文件安装google-drive-ocamlfuse

我按照官方维基上的步骤尝试了从DEB文件安装。

$ sudo apt-get update
$ sudo apt-get install wget
$ wget https://launchpad.net/~alessandro-strada/+archive/ubuntu/ppa/+files/google-drive-ocamlfuse_0.7.30-1ubuntu1_amd64.deb
$ sudo dpkg -i ~/google-drive-ocamlfuse_0.7.30-1ubuntu1_amd64.deb

我在这里遇到了以下错误。

dpkg-deb: error: archive '/home/xxx/google-drive-ocamlfuse_0.7.30-1ubuntu1_amd64.deb' uses unknown compression for member 'control.tar.zst', giving up
dpkg: error processing archive /home/xxx/google-drive-ocamlfuse_0.7.30-1ubuntu1_amd64.deb (--install):
 dpkg-deb --control subprocess returned error exit status 2
Errors were encountered while processing:
 /home/xxx/google-drive-ocamlfuse_0.7.30-1ubuntu1_amd64.deb

我在 ChatGPT 上查询了一下,但是似乎 Debian11 不支持 .tar.zst 格式。

2. 进行GoogleDrive的OAuth认证

如果可以使用图形用户界面(GUI),可以使用浏览器轻松进行身份验证,但本次我们只使用命令行界面(CUI)进行操作。

然而,在官方Wiki中,有一条关于使用桌面应用程序的认证信息的规定。然而,目前这一规定已经无法使用了。

幸运的是,正如该问题所述,已经有一种在网络应用程序中进行认证的方法被实现了,这次我们将使用这个方法。

 

启用Google Drive的API。

在GCP上启用Google Drive API。

創建認證資訊

从GCP的认证信息中,添加以下内容。

Screenshot 2023-05-25 at 16.11.52.png
    • 「OAuthクライアントID」

 

    • アプリケーションの種類: ウェブアプリケーション

 

    • 名前: 任意の表示名

 

    承認済みのリダイレクト URI: http://127.0.0.1:8080/oauth2callback

创建后,将生成客户端ID和客户端密钥。

修改VM上google-drive-ocamlfuse的设置

请注意,google-drive-ocamlfuse的连接设置保存在~/.gdfuse/default/config文件中。(正如后面所提到的,由于有”default”这个标签,可以使用标签来管理多个连接)

在认证过程中,为了能进行重定向,您可以使用vim或其他编辑器来修改VM的~/.gdfuse/default/config文件如下:

- oauth2_loopback=false
+ oauth2_loopback=true

现在我们已经准备好进行认证了。

使用创建的认证信息对虚拟机进行认证。

使用已创建的认证信息,在虚拟机中执行以下命令。

只需一个选项,用中文将以下内容改写为母语:无头,为其提供认证信息进行认证。

google-drive-ocamlfuse -headless -id トークンID -secret クライアントシークレットシークレット

如此显示URL。

Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?XXXXX
Please enter the verification code: 

使用手边的Mac或其他任意浏览器,访问上述网址,在欲使用GoogleDrive的Google账号上进行认证。

一旦认证完成,页面会跳转,您需参考跳转后的URL。
该URL中的code参数将成为应传递给VM的代码。

当URL为http://127.0.0.1:8080/oauth2callback?code=1234&scope=https://www.googleapis.com/auth/drive时,1234为授权代码。

将此代码输入到先前提到的虚拟机终端中。

Please enter the verification code: 1234

如果在终端上正确显示“Access token retrieved correctly.”,则表示认证成功。

使用桌面应用程序的身份验证信息进行身份验证(失败的方法)。

公式维基百科中提到了

创建新的客户端ID时,您必须将应用类型选择为“桌面应用程序”。

一开始,创建了桌面应用的认证信息并进行了连接。

随后,在主机身份验证过程中,出现了以下错误。

Screenshot 2023-05-24 at 15.29.28.png

错误400:invalid_request 为了保护用户安全,已阻止了离线协商(OOB)流程。请按照下面开发者文档中链接的离线协商(OOB)流程迁移指南,将您的应用迁移到另一种方法中。请求详情:redirect_uri=urn:ietf:wg:oauth:2.0:oob

根据链接所示,据说在2022年,不再支持通过手动复制粘贴进行身份认证而无需重定向的方式。

[https://developers.google.com/identity/protocols/oauth2/resources/oob-migration?hl=ja:title]
请用中文原生语言对以上链接进行释义。

[https://developers.google.com/identity/protocols/oauth2/resources/oob-migration?hl=ja:title]
请用中文简洁地概括上述链接。

3. 挂载/卸载GoogleDrive的个人云盘。

默认情况下,将会将已认证的Google账户的My Drive挂载到虚拟机上。
首次挂载时,将会在虚拟机上创建要挂载的目录。

mkdir ~/GoogleDrive

将指定的目录挂载。

google-drive-ocamlfuse GoogleDrive/

卸载该目录

google-drive-ocamlfuse -u GoogleDrive/

使用共享驱动器进行挂载和卸载。

这次我试着把它挂载到共享驱动器上。

在维基百科的公式页面上也有详细步骤。

如何将共享驱动器挂载和卸载

获取要挂载的共享驱动器团队ID。

请显示您想要进行挂载的GoogleDrive的URL,并且URL下的文件夹路径即为团队ID。

例:https://drive.google.com/drive/folders/XXX -> 团队 ID:XXX

将它设置到VM的google-drive-ocamlfuse的配置文件~/.gdfuse/default/config中。

- team_drive_id=
+ team_drive_id=XXX

如果已经将其挂载到另一个驱动器上,则会删除缓存。

fusermount -cc

只要重新挂载,就应该能够查看驱动器的内容了。

使用多个驱动器

另外,如果您有多个驱动器或者共享驱动器,您也可以给它们分别添加标签。
这样一来,配置文件也会分别保存在~/.gdfuse/{label}/config中。

$ google-drive-ocamlfuse -label ラベル名

这就是以上的内容。
有了这个,文件传输工作将会变得更加轻松!

请您查看以下网站

浏览了官方Github,阅读了Wiki和Issue。

 

我在安装Debian时参考了google-drive-ocamlfuse。

 

这是一个连接到AWS虚拟机并进行身份验证的博客。虽然是桌面应用程序认证,但在身份验证方面提供了一些有用的参考。

 

广告
将在 10 秒后关闭
bannerAds