Although there were many uploads to personal G Drive, I posted it because there was no article about shared G-Drive. First post. Way.
--GCP OAuth ID / Password --Access the GCP console --Create a project if there is no project - API & Services > Credential - +Create Credentials > OAuth Client ID --Create Oauth Client ID
pip install PyDrive
--Put the following script settings.yaml
in your working directory
settings.yaml
client_config_backend: settings
client_config:
client_id: <ID>
client_secret: <PASS>
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
test.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
drive_id = "0XXXXXXXXXXk9PVA"
folder_id = "17GBXXXXXXXsCeYFAiUhykVTvuht"
file_metadata = {
'title': directory,
'mimeType': 'application/vnd.google-apps.folder',
'parents': [{
'id': folder_id,
'kind': 'drive#fileLink',
}],
}
f = drive.CreateFile(file_metadata)
f.Upload(param={'supportsTeamDrives': True})
This is miso
f.Upload(param={'supportsTeamDrives': True})
test.py
file_name = "test.csv"
file_metadata = {
'title': "filename_at_gdrive.csv",
'mimeType': 'text/csv',
'parents': [{
'id': folder_id,
'kind': 'drive#fileLink',
}],
}
f = drive.CreateFile(file_metadata)
# use SetContentFile for attach and upload
f.SetContentFile(file_name)
# always apply param when upload
f.Upload(param={'supportsTeamDrives': True})
test.py
query_list = [
"title='{}'".format("2020.csv"),
"mimeType = 'application/vnd.google-apps.folder'",
"'{}' in parents".format(folder_id),
"trashed=false",
]
query = " and ".join(query_list)
# always specifiy team drive when accessing shared drive
file_list = drive.ListFile({
'q':query,
'supportsAllDrives':True,
'corpora': "teamDrive",
'teamDriveId': drive_id,
'includeTeamDriveItems': "true",
'supportsTeamDrives': "true",
}).GetList()
gdrive api method
credentails
How to get folder id
How to store files under folder
support all drive
How to store in a shared driver
mimetype
gdrive query acquisition method