因此,我尝试使用Python和Google Drive API将Google Drive文件夹中的所有文件下载到计算机的文件夹中。但是由于某些原因,下载的文件似乎已损坏,并且大小仅为零字节。我尝试了两种不同的解决方案,但它们都不起作用。有人可以告诉我我在做什么错吗?
from __future__ import print_function
import pickle
from pathlib import Path
import requests
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
import io, os
def dl_file(g_drive, folderID):
folder_path = Path("<folder path>")
files_obj = g_drive.files()
files_list = files_obj.list(pageSize=100,
fields="nextPageToken, files(name, id, webContentLink, parents)").execute()
items = files_list['files']
if not items:
print('No files found.')
else:
for item in items:
if item['parents'] == [folderID]:
file_name = item['name']
#Method 1
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, files_obj.get_media(fileId=item['id']))
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
with io.open(f"{folder_path.as_posix()}/{file_name}", 'wb') as f:
f.seek(0)
f.write(fh.read())
#Method 2
url = item['webContentLink']
r = requests.get(url, stream=True)
if r.ok:
print(f"Saving to '{folder_path.name}/'")
with open(f"{folder_path.as_posix()}/{file_name}", 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 8):
if chunk:
f.write(chunk)
f.flush()
os.fsync(f.fileno())
else: # HTTP status code 4XX/5XX
print("Download failed: status code {}\n{}".format(r.status_code, r.text))
g_drive = build('drive', 'v3', credentials=creds)
dl_file(g_drive, "<folder_ID>")
我相信您的目标如下。
#Method 1
和#Method 2
的2种方法。#Method 1
使用googleapis。#Method 2
使用requests
。为此,这个答案如何?
#Method 1
。当这些内容反映到您的脚本时,它将变成如下。
请如下修改dl_file
。
def dl_file(g_drive, folderID):
folder_path = Path("<folder path>")
files_obj = g_drive.files()
files_list = files_obj.list(pageSize=100,
q="'" + folderID + "' in parents and not mimeType contains 'application/vnd.google-apps'",
fields="nextPageToken, files(name, id, webContentLink, parents)").execute()
items = files_list['files']
if not items:
print('No files found.')
else:
for item in items:
file_name = item['name']
request = g_drive.files().get_media(fileId=item['id'])
fh = io.FileIO(folder_path.as_posix() + '/' + file_name, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
print('Download of ' + file_name)
done = False
while done is False:
status, done = downloader.next_chunk()
print('Download %d%%.' % int(status.progress() * 100))
<folder path>
的目录中。