https://medium.com/analytics-vidhya/how-to-connect-google-drive-to-python-using-pydrive-9681b2a14f20
https://console.developers.google.com/apis/credentials/
google sheet
https://docs.google.com/spreadsheets/d/1q9EJlySLhEqMuJm7bX3NGgys-v_3xqB-nFYwgrNJGM8/edit#gid=0
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
#gauth.CommandLineAuth()
gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
drive = GoogleDrive(gauth)
file = drive.CreateFile({'title': 'My Awesome File.txt'})
file.SetContentString('Hello World!') # this writes a string directly to a file
file.Upload()
https://drive.google.com/drive/folders/11HyAI-T3HE0p1EUkHKWrCcjQkaqxxuhA
Compression
tar cvzf - /var/www/html | split --bytes=99MB - /root/backup.tar.gz.
#backup.tar.gz. will be splitted into backup.tar.gz.a ......backup.tar.gz.b......backup.tar.gz.c
Extraction
cat /root/backup.tar.gz.* | tar xzvf -
finished code:
#!/usr/bin/python3
#!/usr/bin/env python3
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os
import time
#if os.path.exists('/root/py/html.zip') is True:
# os.system("rm -rf /root/py/html.zip")
#else:
# pass
os.system("zip -r /root/py/2025/html.zip /var/www/html")
gauth = GoogleAuth()
gauth.CommandLineAuth()
gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
drive = GoogleDrive(gauth)
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
# This is what solved the issues:
gauth.GetFlow()
gauth.flow.params.update({'access_type': 'offline'})
gauth.flow.params.update({'approval_prompt': 'force'})
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
# Upload files to your Google Drive
#upload_file_list = ['file1.tar.gz','file2.tar.gz']
upload_file_list = ['html.zip']
file_list = drive.ListFile({'q': "'11qyEaFmd3O5c1HmR4be_ea7mvFeXznHm' in parents and trashed=false"}).GetList() #gmail_2025 UbuntuServer folder (11qyEaFmd3O5c1HmR4be_ea7mvFeXznHm)
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
if file1['title'] in upload_file_list:
print("Duplicated files will be removed to Bin and will be removed permanently after 30 days")
file1.Trash()
else:
pass
for upload_file in upload_file_list:
gfile = drive.CreateFile({'parents': [{'id': '11qyEaFmd3O5c1HmR4be_ea7mvFeXznHm'}]}) #upload files to file ID(refer to web page ID): awesome_server folder
# Read file and set it as a content of this instance.
gfile.SetContentFile(upload_file)
gfile.Upload() # Upload the file.
os.system("rm -rf /root/py/2025/html.zip")
Keep settings.yaml file to your directory
client_config_backend: settings
client_config:
client_id: 242687759739-cfvujk63pol45kc8ava01uqu6ho4lapi.apps.googleusercontent.com
client_secret: aiXyN1khd-A8kXJeyHYYnBmj
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.install
Keep client_secrets.json file to your directory
{"installed":{"client_id":"242687759739-cfvujk63pol45kc8ava01uqu6ho4lapi.apps.googleusercontent.com","project_id":"second-abacus-293014","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"aiXyN1khd-A8kXJeyHYYnBmj","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}