简体   繁体   English

如何在 Google Drive Api v3 中进行部分下载?

[英]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?如何使用标头?

  • You want to achieve the partial download of the file from Google Drive using google-api-python-client with python.您想使用 google-api-python-client 和 python 从 Google Drive 实现文件的部分下载。
  • You have already been able to download the file from Google Drive using Drive API with your script.您已经能够使用带有脚本的 Drive API 从 Google Drive 下载文件。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • In this case, the range property like Range: bytes=500-999 is required to be included in the request header.在这种情况下,请求标头中需要包含Range: bytes=500-999Range: bytes=500-999属性。 This has already been mentioned in your question.这已经在你的问题中提到了。
    • For 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.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 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()

Note:笔记:

  • In above modified script, when 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()

Reference:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM