Reputation:
The documentation says here that you need to use the Range header Range: bytes=500-999
.
My code
def downloadChunkFromFile(file_id, start, length):
headers = {"Range": "bytes={}-{}".format(start, start+length)}
#How do I insert the headers?
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request, chunksize=length)
done = False
while done is False:
status, done = downloader.next_chunk()
return fh.getvalue()
How do I use the headers?
Upvotes: 3
Views: 1916
Reputation: 201553
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Range: bytes=500-999
is required to be included in the request header. This has already been mentioned in your question.
request = drive_service.files().get_media(fileId=file_id)
, it includes the range property in the header.When your script is modified, it becomes as follows.
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request, chunksize=length)
done = False
while done is False:
status, done = downloader.next_chunk()
return fh.getvalue()
To:
request = drive_service.files().get_media(fileId=file_id)
request.headers["Range"] = "bytes={}-{}".format(start, start+length)
fh = io.BytesIO(request.execute())
return fh.getvalue()
MediaIoBaseDownload
is used, it was found that the file is completely downloaded without using the range property. So I don't use MediaIoBaseDownload
.Also you can use requests
like as follows.
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
headers = {"Authorization": "Bearer ###accessToken###", "Range": "bytes={}-{}".format(start, start+length)}
res = requests.get(url, headers=headers)
fh = io.BytesIO(res.content)
return fh.getvalue()
If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 2