如何使用python中的请求将大文件上传到Google驱动器?

时间:2019-02-14 20:55:49

标签: python python-2.7 python-requests

我有使用访问令牌和请求将存档上传到Google云端硬盘的代码,但是如果文件大于512MB,它将失败,并显示退出代码 MemoryError ,因此我正在搜索解决此错误并上传大于512MB的文件的方法。我已经尝试找到一种解决方案,但没有找到可以使用访问令牌的任何地方。

import os
import json
import requests
import ntpath
import oauth2
import httplib2
import oauth2client
from contextlib import closing
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials

_CLIENT_ID = 'YOUR_CLIENT_ID'
_CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
_REFRESH_TOKEN = 'YOUR_REFRESH_TOKEN'
_PARENT_FOLDER_ID = 'YOUR_PARENT_FOLDER_ID'
_ARCHIVE_FILE = os.environ['USERPROFILE'] +'\\Desktop\\WobbyChip.zip'


# ====================================================================================

def GetAccessToken(client_id, client_secret, refresh_token):
    cred = oauth2client.client.GoogleCredentials(None,client_id,client_secret,refresh_token,None,'https://accounts.google.com/o/oauth2/token',None)
    http = cred.authorize(httplib2.Http())
    cred.refresh(http)
    obj = json.loads(cred.to_json())
    _ACCESS_TOKEN = obj['access_token']
    return _ACCESS_TOKEN


def UploadFile(local_file, parent_folder_id, access_token,):
    headers = {'Authorization': 'Bearer ' +access_token}
    para = {
        'name': (ntpath.basename(local_file)),
        'parents': [parent_folder_id]}
    files = {
        'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
        'file': open(local_file, 'rb')}
    requests.post(
        'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
        headers=headers,
        files=files)

# ====================================================================================


if __name__ == '__main__':
    UploadFile(_ARCHIVE_FILE, _PARENT_FOLDER_ID, GetAccessToken(_CLIENT_ID, _CLIENT_SECRET, _REFRESH_TOKEN))

1 个答案:

答案 0 :(得分:0)

我认为您需要使用分块上传。它试图在一个实例中打开和上传文件,并且用完了内存