服务器(终端)下载 Google Drive 上面的数据

本文介绍了一种从GoogleDrive直接下载文件至服务器的方法,通过使用Python脚本简化了下载流程,避免了本地中转的繁琐步骤。文章提供了脚本源码及详细使用教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##  服务器(终端)下载 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
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值