[英]How to do a partial download in Google Drive Api v3?
The documentation says here that you need to use the Range header Range: bytes=500-999
.文档在这里说您需要使用 Range 标头
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?如何使用标头?
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.Range: bytes=500-999
等Range: bytes=500-999
属性。 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.request = drive_service.files().get_media(fileId=file_id)
,它在标头中包含范围属性。 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.MediaIoBaseDownload
时,发现文件是完全下载的,没有使用 range 属性。 So I don't use MediaIoBaseDownload
.MediaIoBaseDownload
。 Also you can use requests
like as follows.您也可以使用如下
requests
。
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.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.