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