【问题标题】:Download Shared Google Drive Folder with Python使用 Python 下载共享的 Google Drive 文件夹
【发布时间】:2019-07-02 18:02:44
【问题描述】:

我在谷歌驱动器上有一个只有 .jpg 图像的文件夹,我想使用该文件夹的共享链接将文件夹中的所有图像下载到我的计算机。

到目前为止,我发现唯一有效的是下面的代码,但我只能让它适用于特定的共享文件,而不是整个文件夹。

from google_drive_downloader import GoogleDriveDownloader as gdd

gdd.download_file_from_google_drive(file_id='1viW3guJTZuEFcx1-ivCL2aypDNLckEMh',
                                    dest_path='./data/mnist.zip',
                                    unzip=True)

有没有办法修改它以使用 google 文件夹,或者是否有其他方法可以下载 google drive 文件夹?

【问题讨论】:

  • 我想你的问题在这里得到了回答:stackoverflow.com/questions/38511444/…
  • @aseembhartiya 我也试过了,但它只适用于特定文件,我需要下载整个文件夹。
  • 我能问一下你的情况吗?您想下载共享文件夹中的所有文件。如果我的理解是正确的,您是否需要使用python?例如,有一个用于从共享文件夹下载所有文件的 CLI 工具。这个怎么样?如果我误解了您的问题,我深表歉意。
  • @Tanaike 我只需要某种方式让 python 运行程序从共享的谷歌驱动器文件夹下载文件。它将在 Raspberry Pi 上运行
  • @Brandalf 感谢您的回复。我不确定这个 CLI 工具是否可以用于您的情况,这个怎么样? youtube.com/watch?v=ncYzrwTGaJA

标签: python download google-drive-api


【解决方案1】:

如果您使用 Google Drive API which you can find here 的 Python 快速入门教程,您将能够使用 API 设置您的身份验证。然后,您可以通过将搜索的 MIMEType 指定为 image/jpeg 来循环浏览您的云端硬盘并仅下载 jpg 文件。

首先,您要遍历驱动器上的文件using files: list

# Note: folder_id can be parsed from the shared link
def listFiles(service, folder_id):
    listOfFiles = []

    query = f"'{folder_id}' in parents and mimeType='image/jpeg'"

    # Get list of jpg files in shared folder
    page_token = None
    while True:
        response = service.files().list(
            q=query,
            fields="nextPageToken, files(id, name)",
            pageToken=page_token,
            includeItemsFromAllDrives=True, 
            supportsAllDrives=True
        ).execute()

        for file in response.get('files', []):
            listOfFiles.append(file)

        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    return listOfFiles

然后你可以使用files: get_media()方法下载它们:

import io
from googleapiclient.http import MediaIoBaseDownload

def downloadFiles(service, listOfFiles):
    # Download all jpegs
    for fileID in listOfFiles:
        request = service.files().get_media(fileId=fileID['id'])
        fh = io.FileIO(fileID['name'], 'wb')
        downloader = MediaIoBaseDownload(fh, request)

        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Downloading..." + str(fileID['name']))

您可以使用listFiles() 中指定的here 中的q 参数进一步优化搜索。

【讨论】:

  • 感谢您的建议,但我尝试制作的程序只能使用来自共享 google 驱动器文件夹的链接。
  • 我已更新我的答案,以包含用于在 Files: list() 中搜索特定共享云端硬盘的参数。确保将 'SHARED_DRIVE_ID' 更改为您共享云端硬盘的 ID。
【解决方案2】:

您可以使用gdrive 工具。基本上它是一个从命令行访问谷歌驱动器帐户的工具。按照以下示例为 Linux 机器进行设置:

  1. 首先下载gdrive的执行文件here
  2. 解压得到可执行文件gdrive。现在通过执行命令更改文件的权限 chmod +x gdrive
  3. 运行./gdrive about,你会得到一个要求你输入验证码的URL。 按照提示复制链接并转到您的浏览器上的 URL,然后登录您的 Google Drive 帐户并授予权限。最后你会得到一些验证码。复制它。
  4. 回到之前的终端,粘贴刚才复制的验证码。然后验证 你的信息在那里。现在您已成功将您的机器链接到您的 Google 云端硬盘 帐户。

现在完成上述过程后,您可以使用下面提到的命令浏览驱动器上的文件。

./gdrive list # List all files' information in your account
./gdrive list -q "name contains 'University'" # serch files by name
./gdrive download fileID # Download some file. You can find the fileID from the 'gdrive list' result.
./gdrive upload filename  #  Upload a local file to your google drive account.
./gdrive mkdir # Create new folder

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2016-07-31
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多