有没有办法通过curl或wget下载可公开查看的Google Drive URL?例如,能够执行以下操作:
curl -O myfile.xls https://drive.google.com/uc?export=download&id=1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl
请注意,我希望在可公开查看的文件上执行此操作,而无需登录我的 Google 帐户(或让其他人登录他们的帐户等)。
如果有帮助的话,我的 cors header 是:
"kind": "drive#file",
"id": "1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl",
最佳答案
这个方法怎么样?当文件太大时,Google 会返回下载该文件的代码。您可以使用代码下载该文件。当使用curl下载这么大的文件时,可以看到如下代码。
<a id="uc-download-link" class="goog-inline-block jfk-button jfk-button-action" href="/uc?export=download&confirm=ABCD&id=### file ID ###">download</a>
带有confirm=ABCD
的查询对于下载文件很重要。该代码也包含在 cookie 中。在cookie中,可以看到如下。
#HttpOnly_.drive.google.com TRUE /uc TRUE ##### download_warning_##### ABCD
在本例中,“ABCD”是代码。为了从 cookie 中检索代码并下载文件,您可以使用以下脚本。
示例脚本:
#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
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}
如果这对您没有用,我很抱歉。
更新于 2022 年 2 月 17 日
最近,该流程的规范似乎已更改。所以我更新了这个答案。 要从 Google 云端硬盘下载公开共享的大文件,您可以使用以下脚本。
#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}"`
curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Po '(confirm=[a-zA-Z0-9\-_]+)'`&id=${fileid}" -o ${filename}
在这种情况下,从 HTML 数据中检索用于下载的 ID,如下所示。
<form id="downloadForm" action="https://drive.google.com/uc?export=download&id={fileId}&confirm={value for downloading}" method="post">
当您想从 Google 云端硬盘下载公开共享的小文件时,可以使用以下命令。
curl -L "https://drive.google.com/uc?export=download&id=### fileId ###" -o sampleoutput.csv
关于curl - 如何通过curl或wget下载Google Drive URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133080/