I cannot see your code, so I’ll provide you some general information on some options that can help you solve the issue. You can implement downloading the file in Chunks using MediaIoBaseDownload
, you can see some documentation about this here.
Example:
request = farms.animals().get_media(id='cow')
fh = io.FileIO('cow.png', mode='wb')
downloader = MediaIoBaseDownload(fh, request, chunksize=1024*1024)
done = False
while done is False:
status, done = downloader.next_chunk()
if status:
print "Download %d%%." % int(status.progress() * 100)
print "Download Complete!"
Get the next chunk of the download.
Args: num_retries: Integer, number of times to retry with randomized
exponential backoff. If all retries fail, the raised HttpError
represents the last request. If zero (default), we attempt the
request only once.
Returns: (status, done): (MediaDownloadProgress, boolean)
The value of 'done' will be True when the media has been fully
downloaded or the total size of the media is unknown.
Raises: googleapiclient.errors.HttpError if the response was not a
2xx. httplib2.HttpLib2Error if a transport error has occurred.
I also found this example in the Google documentation here.
from __future__ import print_function
import io
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
def download_file(real_file_id):
"""Downloads a file
Args:
real_file_id: ID of the file to download
Returns : IO object with location.
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
creds, _ = google.auth.default()
try:
# create drive api client
service = build('drive', 'v3', credentials=creds)
file_id = real_file_id
# pylint: disable=maybe-no-member
request = service.files().get_media(fileId=file_id)
file = io.BytesIO()
downloader = MediaIoBaseDownload(file, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(F'Download {int(status.progress() * 100)}.')
except HttpError as error:
print(F'An error occurred: {error}')
file = None
return file.getvalue()
if __name__ == '__main__':
download_file(real_file_id='1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9')
Lastly, you can review several examples on how to use MediaIoBaseDownload
with chunks in these 2 blogs.
- Python googleapiclient.http.MediaIoBaseDownload() Examples
- googleapiclient.http.MediaIoBaseDownload
Update
Partial download functionality is provided by many client libraries via a Media Download service. You can refer to the client library documentation for details here and here. However, the documentation is not very clear.
The API client library for Java has more information an states that:
"The resumable media download protocol is similar to the resumable media upload protocol, which is described in the Google Drive API documentation."
In the Google Drive API documentation you will find some examples using python for resumable upload. You can use the documentation of the Python google-resumable-media library, the Java resumable media download, and the resumable upload as base for the code to restart the upload once it fails.