【问题标题】:Download an entire public folder from Google-Drive using Python or wget/curl without authentication使用 Python 或 wget/curl 从 Google-Drive 下载整个公用文件夹,无需身份验证
【发布时间】:2021-08-28 22:48:35
【问题描述】:

我想通过脚本(python、wget、终端等)从 Google-drive 下载整个公用文件夹。
该过程无需身份验证即可完成,因为它是一个公共文件夹,任何拥有链接的人都可以访问。

链接示例:https://drive.google.com/drive/folders/1Gt-W8jMrADizXYGF5QZwJh_Gc8QpKflX

如果无法直接下载整个文件夹,那么只需列出其内容(文件)就足够了,无需身份验证,然后我就可以单独下载每个文件。如何获得这样的listing功能?

注意
我发现了许多类似的讨论,但都假设文件下载或身份验证,我找不到该特定问题的匹配项,例如:

【问题讨论】:

  • 例如,在您的情况下,这个 CLI 工具有用吗? github.com/tanaikech/goodls 但是,当公用文件夹中的文件时,至少需要使用 API 密钥,因为需要从公用文件夹中检索文件列表。如果这没有用,我很抱歉。
  • 您好 Tanaike,这似乎确实相关,我不知道那个 CLI。我试试看,谢谢!
  • 感谢您的回复。如果这对您的情况没有用,我深表歉意。而且,如果您不能使用 API 密钥,例如,如何通过手动操作或 Google Apps 脚本从公用文件夹创建文件列表?这样,您可以在没有 API 密钥的情况下下载文件。
  • 这是个好主意,感谢 Tanaike。在您的启发下,我还找到了这个 Google Apps 脚本,谢谢 --> gist.github.com/mesgarpour/07317e81e9ee2b3f1699
  • 感谢您的回复。如果您的问题已解决,您可以将其发布为答案吗?这样,它对遇到相同问题的其他用户很有用。

标签: download python-requests google-drive-api google-drive-shared-drive


【解决方案1】:

我最终应用了以下代码(经过测试,运行良好):

import urllib.request
from getfilelistpy import getfilelist
from os import path, makedirs, remove, rename

def download_googledrive_folder(remote_folder, local_dir, gdrive_api_key, debug_en):

    success = True

    if debug_en:
        print('[DEBUG] Downloading: %s --> %s' % (remote_folder, local_dir))
    else:
        try:
            resource = {
                "api_key": gdrive_api_key,
                "id": remote_folder.split('/')[-1].split('?')[0],
                "fields": "files(name,id)",
            }
            res = getfilelist.GetFileList(resource)
            print('Found #%d files' % res['totalNumberOfFiles'])
            destination = local_dir
            if not path.exists(destination):
                makedirs(destination)
            for file_dict in res['fileList'][0]['files']:
                print('Downloading %s' % file_dict['name'])
                if gdrive_api_key:
                    source = "https://www.googleapis.com/drive/v3/files/%s?alt=media&key=%s" % (file_dict['id'], gdrive_api_key)
                else:
                    source = "https://drive.google.com/uc?id=%s&export=download" % file_dict['id']  # only works for small files (<100MB)
                destination_file = path.join(destination, file_dict['name'])
                urllib.request.urlretrieve(source, destination_file)

        except Exception as err:
            print(err)
            success = False

    return success

我找不到实现我最初目标的方法,即在没有任何凭据/密钥的情况下从 Google-Drive 下载公共文件夹,但上面的代码对我来说是一个很好的折衷方案,因为它只需要一个密钥而不是完整的凭据.

请注意,这里有 2 个选项 --> 为源 URL 提供或不提供 Google API 密钥 (gdrive_api_key)。
根据我的经验,不带 API 密钥的选项适用于小文件(

【讨论】:

  • 什么是getfilelistpy?
  • 嗨,Mehmet,请参阅pypi.org/project/getfilelistpy
  • 非常感谢,我以为它是您本地的 py 文件,而不是开源项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 2015-08-17
  • 1970-01-01
相关资源
最近更新 更多