PyDrive icon indicating copy to clipboard operation
PyDrive copied to clipboard

How can I upload a file to team drives folder?

Open mythicc opened this issue 5 years ago • 12 comments

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.

mythicc avatar Nov 02 '18 02:11 mythicc

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

spartako avatar Dec 03 '18 12:12 spartako

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 ?

chrisPiemonte avatar Jan 16 '19 21:01 chrisPiemonte

@spartako How do you find the team drive id?

bendavis78 avatar Jan 25 '19 23:01 bendavis78

@bendavis78 The ID of the Team Drive can be found at the last part of its URL

Imoustak avatar Feb 04 '19 15:02 Imoustak

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

rlkc112358 avatar Feb 08 '19 03:02 rlkc112358

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

hermannsblum avatar Feb 26 '19 22:02 hermannsblum

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 avatar Feb 27 '19 12:02 rarup1

@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!

keliomer avatar May 24 '19 00:05 keliomer

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

Berti30 avatar Nov 18 '19 13:11 Berti30

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: image

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 avatar Apr 19 '21 17:04 cccat6

@cccat6 you could also check the maintained PyDrive2 fork. It has that (and many other fixes) in it.

shcheklein avatar Apr 19 '21 17:04 shcheklein

@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.

cccat6 avatar Apr 19 '21 18:04 cccat6