PyDrive
PyDrive copied to clipboard
How can I upload a file to team drives folder?
I already made a python script to upload file to specified folder, but I can't find the team drive folder by id.
Can anyone give me a hint? Thanks.
Just make sure your credentials have access to that Team Drive, and then add a few more parameters like so:
file_list = drive.ListFile(
{
'q': "'root' in parents and trashed=false",
'corpora': "teamDrive",
'teamDriveId': "YOUR TEAM DRIVE ID HERE",
'includeTeamDriveItems': "true",
'supportsTeamDrives': "true"
}
).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
Once you get the ID, the process for uploading is as usual
I still can't upload files to team drives folder, but i can list them. This is my code to upload files:
f = drive.CreateFile({
'supportsTeamDrives': 'true',
'teamDriveId': 'XXXXXXXXXXXXX',
'corpora': 'teamDrive',
'name' : 'test',
'includeTeamDriveItems': 'true',
'mimeType' : 'application/vnd.google-apps.folder',
'parents': [{
'kind': 'drive#fileLink',
'id': 'XXXXXXXXXXXXX',
'teamDriveId': 'XXXXXXXXXXXXX'
}]
})
f.Upload()
I don't know if this is because of permissions or else, and i don't know how to check it either. Can anyone help me on this ?
@spartako How do you find the team drive id?
@bendavis78 The ID of the Team Drive can be found at the last part of its URL
Credits to this StackOverflow Answer
You have put the supportsTeamDrives:"true" in the wrong place
the docs https://developers.google.com/drive/api/v2/reference/files/insert specify that it needs to be a query parameter
Here is my code which worked
team_drive_id = 'XXXXXXXXXXXXX'
parent_folder_id = 'XXXXXXXXXXXXX'
f = drive.CreateFile({
'title': 'test.txt',
'parents': [{
'kind': 'drive#fileLink',
'teamDriveId': team_drive_id,
'id': parent_folder_id
}]
})
f.SetContentString('Hello World')
f.Upload(param={'supportsTeamDrives': True})
This seems the right place to ask:
How can I download a file? Unfortunately, just adding the supportsTeamDrives
flag does not help...
downloaded = drive.CreateFile({'id': file_id, 'supportsTeamDrives': True})
print('Downloaded content "{}"'.format(downloaded.GetContentString()))
I am having the same problem. I have searched everywhere but no luck so far. The GET
request when I set up my CreateFile as follows seem to omit the teamSupport params:
file = self.drive.CreateFile(
{
'id': fileid,
'supportsTeamDrives': 'true',
'includeTeamDriveItems': 'true'
})
This is the Get request captured in my logger:
INFO:googleapiclient.discovery:URL being requested: GET https://www.googleapis.com/drive/v2/files/1omHKJUACcnyqaSaMJ3kvVsQ3U8E3Cm5Y?alt=json
I tried setting up as suggested with the nested params to no avail too:
team_drive_id = XXXXXXX
file = self.drive.CreateFile(
{
'id': fileid,
'parents': [{
'kind': 'drive#fileLink',
'teamDriveId': team_drive_id
}]
})
Like the above commenter said the
drive.ListFile(
}).GetList()
works fine.
Any help in getting it to send the correct Get request would be much appreciated.
Thanks!
@rarup1 you need to add query parameters to the ListFile() call
ie
file_list = drive.ListFile({'q':"title='yourfiletitle' and 'parent_id' in parents and trashed=false"}).GetList()
Without the query params you're not actually looking for anything!
I have the exact same question as @rarup1. I know @fergusleahytab has brought modifications to PyDrive, but I am still not sure how to modify @rarup1's code to download files from a shared folder (where to put 'supportsTeamDrives': True if there any need for it). Any help would be deeply appreciated.
Kind regards, Berti
I successfully upload the files to Shared Drive (Team Drive) with PyDrive.
In pydrive/files.py, about line 368. Add a line of code param['supportsAllDrives'] = True
Which should look like this:
Last, just simply use these code templet
f = drive.CreateFile({'title': '<file name, not really necessary>','parents': [{'id': '<your shared drive id>'}]})
f.SetContentFile(os.path.join(path, '<file in your local>'))
f.Upload()
Hopes it will help someone.
@cccat6 you could also check the maintained PyDrive2 fork. It has that (and many other fixes) in it.
@cccat6 you could also check the maintained PyDrive2 fork. It has that (and many other fixes) in it.
Ops... I didn't notice there is a PyDrive2. Spends me an hour on positioning the point :( Thank you for your inform.