如何使用python中的请求将大文件上传到谷歌驱动器? [英] How to upload big file to google drive using requests in python?

查看:73
本文介绍了如何使用python中的请求将大文件上传到谷歌驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have code that uploads my archive to Google Drive using my access token and requests, but if the file is larger than 512MB, it will fail with exit code MemoryError, so I'm searching for a way to fix this error and upload a file larger than 512MB. I already tried to find a solution but I didn't find anything where I could use an access token.

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

推荐答案

我认为您需要使用分块上传.它试图在一个实例中打开并上传文件并且内存不足

I think you will need to use chunked upload. it's trying to open and upload the file in one instance and running out of memory

这篇关于如何使用python中的请求将大文件上传到谷歌驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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