2

如何从 下载文件googledrive

我正在使用pydrive链接。

#https://drive.google.com/open?id=DWADADDSASWADSCDAW
    from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

gdrive_file = drive.CreateFile({'id': 'id=DWADADDSASWADSCDAW'})
gdrive_file.GetContentFile('DWADSDCXZCDWA.zip') # Download content file.

错误:

raceback (most recent call last):
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
    with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 386, in LoadClientConfigFile
    client_type, client_info = clientsecrets.loadfile(client_config_file)
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
    exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:/Users/Hoxton/123/pyu_test.py", line 8, in <module>
        gdrive_file.GetContentFile('PyUpdater+App-win-1.0.zip') # Download content file.
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\files.py", line 210, in GetContentFile
        self.FetchContent(mimetype, remove_bom)
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\files.py", line 42, in _decorated
        self.FetchMetadata()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 57, in _decorated
        self.auth.LocalWebserverAuth()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 113, in _decorated
        self.GetFlow()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 443, in GetFlow
        self.LoadClientConfig()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 366, in LoadClientConfig
        self.LoadClientConfigFile()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 388, in LoadClientConfigFile
        raise InvalidConfigError('Invalid client secrets file %s' % error)
    pydrive.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

    Process finished with exit code 1
4

2 回答 2

2

尝试文档中提供的示例代码。

Drive API 允许您下载存储在 Google Drive 中的文件。此外,您可以下载您的应用可以处理的格式的 Google 文档(文档、电子表格、演示文稿等)的导出版本。webViewLinkDrive 还支持通过属性中的 URL 让用户直接访问文件。

这是代码片段

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)
于 2017-11-28T11:32:01.213 回答
-1

嘿,我知道回答有点晚了,但它可能仍然对某人有帮助。

我对 G-sheets 也有类似的问题,这里的问题是可能有多种格式可以下载文件,而您没有指定要使用哪种格式。为此,您需要将 mimetype 参数添加到 GetContentFile 方法. 像这样:

gdrive_file.GetContentFile('DWADSDCXZCDWA.zip', mimetype = 'application/zip')

请注意,zip 文件有多种 mimetype,并且 mimetype 和扩展名需要一致。所以你需要知道使用哪一个,或者如果你不使用,就尝试不同的。这是一个方便的清单:

  • 应用程序/x-压缩
  • 应用程序/x-zip-压缩
  • 应用程序/压缩包
  • 多部分/x-zip

此外,如果您实际访问文件的元数据,您可以查看可以在“exportLinks”下导出的所有格式类型。将有一个带有 mimetypes 和相关链接的字典。

于 2021-05-05T10:41:49.220 回答