中小型文件 - 使用gdown
安装gdown
pip install gdown
获取Google drive文件(不能是文件夹)的ID
复制到的链接大概长下面这个样子,其中加粗的部分就是文件的ID
https://drive.google.com/file/d/12LhpGHvGu4wIgXrONspNKqfGFpTfr0-p/view?usp=drive_link
在Python中运行(注意下面的url
和上面的不太一样)
>>> import gdown
>>> url = 'https://drive.google.com/uc?id=12LhpGHvGu4wIgXrONspNKqfGFpTfr0-p'
>>> output = 'file_name.txt'
>>> gdown.download(url, output, quiet=False)
这个方法对中小型文件可行,但对过大的文件(以上图中40GB的文件为例,文件ID
是1CDuzEbjK1hPbMItoJwrLIMgK25FSRM6_
),会报下面的错
Access denied with the following error:
Too many users have viewed or downloaded this file recently. Please
try accessing the file again later. If the file you are trying to
access is particularly large or is shared with many people, it may
take up to 24 hours to be able to view or download the file. If you
still can't access a file after 24 hours, contact your domain
administrator.
You may still be able to access the file from the browser:
https://drive.google.com/uc?id=1CDuzEbjK1hPbMItoJwrLIMgK25FSRM6_
这时,就需要使用Token和命令行下载(以下内容参考自stackoverflow)。
大型文件 - 命令行
-
找到这一条并点击,然后点下面的
Authorize APIs
-
再点击出现的
Exchange authorization code for tokens
-
复制生成的
Access Toekn
-
在终端运行(注意,下面的
ACCESS_TOKEN
要换成刚才复制的,FILE_ID
改成要下载的文件ID
,FILE_NAME
改成要保存的文件名)
curl -H "Authorization: Bearer ACCESS_TOKEN" https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media -o FILE_NAME
然后就开始漫长的传输了。