如何在Google Drive Api v3中进行部分下载? [英] How to do a partial download in Google Drive Api v3?

查看:125
本文介绍了如何在Google Drive Api v3中进行部分下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档此处范围标头Range: bytes=500-999.

我的代码

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()

我如何使用标题?

推荐答案

  • 您要使用带有python的google-api-python-client从Google Drive实现文件的部分下载.
  • 您已经能够使用带有脚本的Drive API从Google云端硬盘下载文件.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    • 在这种情况下,要求标头中包含诸如Range: bytes=500-999之类的range属性.您的问题中已经提到了这一点.
      • 对于request = drive_service.files().get_media(fileId=file_id),它在标头中包含range属性.
      • In this case, the range property like Range: bytes=500-999 is required to be included in the request header. 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.

        修改脚本后,它如下所示.

        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()
        

        到:

        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时,发现文件完全下载而不使用range属性.所以我不使用MediaIoBaseDownload.
        • 此外,您也可以按如下方式使用requests.

          Note:

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

            这篇关于如何在Google Drive Api v3中进行部分下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆