我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的uid,较小的文件(1.6MB)下载正常,但较大的文件(3.7GB)总是重定向到一个页面,询问我是否想在不进行病毒扫描的情况下继续下载。谁能帮我跳过那个屏幕?
下面是我如何让第一个文件工作-
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYeDU0VDRFWG9IVUE" > phlat-1.0.tar.gz
当我对另一个文件进行同样操作时,
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYY3h5YlMzTjhnbGM" > index4phlat.tar.gz
我得到以下输出-
我注意到在链接的第三行到最后一行,有一个&confirm=JwkK,这是一个随机的4个字符的字符串,但建议有一种方法添加到我的URL确认。我访问的一个链接建议&confirm=no_antivirus,但这不起作用。
我希望这里有人能帮忙!
截至2018年3月更新。
我尝试了其他答案中给出的各种技术,直接从谷歌驱动器下载我的文件(6 GB)到我的AWS ec2实例,但没有一个有效(可能是因为它们太旧了)。
所以,为了让其他人知道,下面是我是如何成功做到的:
Right-click on the file you want to download, click share, under link sharing section, select "anyone with this link can edit".
Copy the link. It should be in this format: https://drive.google.com/file/d/FILEIDENTIFIER/view?usp=sharing
Copy the FILEIDENTIFIER portion from the link.
Copy the below script to a file. It uses curl and processes the cookie to automate the downloading of the file.
#!/bin/bash
fileid="FILEIDENTIFIER"
filename="FILENAME"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}
As shown above, paste the FILEIDENTIFIER in the script. Remember to keep the double quotes!
Provide a name for the file in place of FILENAME. Remember to keep the double quotes and also include the extension in FILENAME (for example, myfile.zip).
Now, save the file and make the file executable by running this command in terminal sudo chmod +x download-gdrive.sh.
Run the script using `./download-gdrive.sh".
PS:这是上面给出的脚本的Github要点:https://gist.github.com/amit-chahar/db49ce64f46367325293e4cce13d2424
以上所有的回答似乎都掩盖了答案的简单性,或者有一些没有解释的细微差别。
如果文件是公开共享的,您只需知道文件ID就可以生成一个直接下载链接。URL必须是“https://drive.google.com/uc?id=[FILEID]&export=download”的形式。此格式自2019年11月22日起生效。这并不要求接收方登录到谷歌,但要求公开共享该文件。
在浏览器中,导航到drive.google.com。
右键点击文件,点击“获取可共享链接”
打开一个新选项卡,选择地址栏,并粘贴到剪贴板的内容,这将是可共享的链接。您将看到谷歌的查看器显示的文件。ID是URL的“View”组件前面的数字:
编辑URL,使其为以下格式,将“[FILEID]”替换为共享文件的ID:
https://drive.google.com/uc?id=[文件标识]进出口=下载
这是你的直接下载链接。如果你在浏览器中点击它,文件现在会被“推送”到你的浏览器,打开下载对话框,允许你保存或打开文件。您也可以在下载脚本中使用此链接。
所以等价的curl命令是:
curl -L "https://drive.google.com/uc?id=AgOATNfjpovfFrft9QYa-P1IeF9e7GWcH&export=download" > phlat-1.0.tar.gz
获取文件ID:
1.在浏览器中打开谷歌驱动器。
2.右键单击要下载的文件,单击“获取可共享链接”。链接如下所示:https://drive.google.com/file/d/XXX/view?usp=sharing。记录文件ID XXX;你将在下面需要它。
获取一个OAuth令牌:
1.去OAuth 2.0游乐场
2.在“选择和授权API”框中,向下滚动,展开Drive API v3,并选择https://www.googleapis.com/auth/drive.readonly。
3.单击“授权api”,然后为令牌交换授权代码。复制Access令牌YYY;你将在下面需要它。
从命令行下载文件:
如果操作系统为OS X或Linux,打开“终端”程序,输入以下命令。
curl -H "Authorization: Bearer YYY" https://www.googleapis.com/drive/v3/files/XXX?alt=media -o ZZZ
如果使用Windows操作系统,打开PowerShell程序,输入以下命令。
Invoke-RestMethod -Uri https://www.googleapis.com/drive/v3/files/XXX?alt=media -Method Get Headers @{"Authorization"="Bearer YYY"} -OutFile ZZZ
在您的命令中,将XXX替换为上面的文件ID, YYY替换为上面的访问令牌,ZZZ替换为将保存的文件名(例如,如果您下载的是zip文件,则替换为“myFile.zip”)。
我一直在使用@Amit Chahar的curl片段,他在这个帖子中给出了一个很好的答案。我发现它很有用
将其放在bash函数中,而不是单独的.sh文件中
function curl_gdrive {
GDRIVE_FILE_ID=$1
DEST_PATH=$2
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${GDRIVE_FILE_ID}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${GDRIVE_FILE_ID}" -o ${DEST_PATH}
rm -f cookie
}
可以包含在例如a ~/。Bashrc(当然,如果不是自动源),并以以下方式使用
$ curl_gdrive 153bpzybhfqDspyO_gdbcG5CMlI19ASba imagenet.tar
UPDATE 2022-03-01 - wget版本,当病毒扫描被触发时也可以工作
function wget_gdrive {
GDRIVE_FILE_ID=$1
DEST_PATH=$2
wget --save-cookies cookies.txt 'https://docs.google.com/uc?export=download&id='$GDRIVE_FILE_ID -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p' > confirm.txt
wget --load-cookies cookies.txt -O $DEST_PATH 'https://docs.google.com/uc?export=download&id='$GDRIVE_FILE_ID'&confirm='$(<confirm.txt)
rm -fr cookies.txt confirm.txt
}
示例用法:
$ wget_gdrive 1gzp8zIDo888AwMXRTZ4uzKCMiwKynHYP foo.out