## 服务器(终端)下载 Google Drive 上面的数据
之前在终端下载google drive上的数据都是先下载到本地,再上传,发现太麻烦了,今天找到一种方法分享给大家,
可以去github上下载下载的python脚本:https://github.com/chentinghao/download_google_drive/blob/master/download_gdrive.py
因为代码比较短,我粘贴过来:
import requests
from tqdm import tqdm
def download_file_from_google_drive(id, destination):
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
with tqdm(unit='B', unit_scale=True, unit_divisor=1024) as bar:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
bar.update(CHUNK_SIZE)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
if __name__ == "__main__":
import sys
if len(sys.argv) is not 3:
print("Usage: python google_drive.py drive_file_id destination_file_path")
else:
# TAKE ID FROM SHAREABLE LINK
file_id = sys.argv[1]
# DESTINATION FILE ON YOUR DISK
destination = sys.argv[2]
download_file_from_google_drive(file_id, destination)
然后可以直接运行这个脚本;
python download_gdrive.py GoogleFileID /path/xxxx.file
其中GoogleFileID 就是url中的id,/path/xxxx.file就是存放文件的位置和文件名,
获取GoogleFileID如下:
单击右键:
点击“获取共享链接”,
复制url,可以得到如下:
https://drive.google.com/open?id=1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d
那么GoogleFileID就是id后面的那一串,也就是 1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d
那么运行脚本就是:
python download_gdrive.py 1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d /path/download.file