Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def upload1(file):
- from pydrive.auth import GoogleAuth
- from pydrive.drive import GoogleDrive
- settings_path = 'settings.yaml'
- gauth = GoogleAuth(settings_file=settings_path)
- gauth.LoadCredentialsFile("mycreds.txt")
- if gauth.credentials is None:
- gauth.LocalWebserverAuth()
- elif gauth.access_token_expired:
- gauth.Refresh()
- else:
- gauth.Authorize()
- gauth.SaveCredentialsFile("mycreds.txt")
- drive = GoogleDrive(gauth)
- team_drive_id = 'my_team_drive_id1'
- list_file_q = "{'q': \"'" + team_drive_id + "' in parents and trashed=False\", 'supportsAllDrives': True, 'driveId': '" + team_drive_id + "', 'includeItemsFromAllDrives': True, 'corpora': 'drive'}"
- print("list_file_q: \n")
- print(list_file_q)
- print("\n")
- file_list = drive.ListFile(eval(list_file_q)).GetList()
- print("file_list:\n")
- print(file_list)
- print("\n")
- file_id = ""
- try:
- for file1 in file_list:
- print("file title: \n")
- print(file1['title'])
- print(file1['id'])
- print(file)
- print("\n")
- if file1['title'] == file:
- file_id = file1['id']
- print("file names do match, thus deleting... \n")
- file1.Delete()
- file1.Trash()
- drive.CreateFile({'id': file1['id']}).Delete()
- else:
- print("file names do NOT match, thus NOT deleting. \n")
- except:
- pass
- if not file_id:
- print("creating file from scratch... \n")
- f = drive.CreateFile({'parents': [{'kind': 'drive#fileLink', 'teamDriveId': team_drive_id, 'id': team_drive_id}]})
- else:
- print("updating file using existing file_id... \n")
- var_create_file = "{'id': " + file_id + ", 'parents': [{'kind': 'drive#fileLink', 'teamDriveId': " + team_drive_id + ", 'id': " + team_drive_id + "}]}"
- print("var_create_file: \n")
- print(var_create_file)
- print("\n")
- f = drive.CreateFile(eval(var_create_file))
- print("test after create file \n")
- print("before SetContentFile \n")
- f.SetContentFile(file)
- print("after SetContentFile \n")
- print("before f.Upload \n")
- f.Upload(param={'supportsTeamDrives': True})
- print("after f.Upload \n")
Advertisement
Add Comment
Please, Sign In to add comment