Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

How can I upload a file to team drives folder? #149

Open
mythicc opened this issue Nov 2, 2018 · 12 comments · May be fixed by #178
Open

How can I upload a file to team drives folder? #149

mythicc opened this issue Nov 2, 2018 · 12 comments · May be fixed by #178

Comments

@mythicc
Copy link

mythicc commented Nov 2, 2018

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.

@spartako
Copy link

spartako commented Dec 3, 2018

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

@chrisPiemonte
Copy link

chrisPiemonte commented Jan 16, 2019

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 ?

@bendavis78
Copy link

@spartako How do you find the team drive id?

@Imoustak
Copy link

Imoustak commented Feb 4, 2019

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

@rlkc112358
Copy link

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

@hermannsblum
Copy link

hermannsblum commented Feb 26, 2019

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

@rarup1
Copy link

rarup1 commented Feb 27, 2019

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!

@keliomer
Copy link

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

@Berti30
Copy link

Berti30 commented Nov 18, 2019

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

@cccat6
Copy link

cccat6 commented Apr 19, 2021

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.

@shcheklein
Copy link
Collaborator

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

@cccat6
Copy link

cccat6 commented Apr 19, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.