5

I know how to use PyDrive to download a file from my drive, the problem is that I need to download (or at the very least OPEN) an xlsx file on a shared drive. Here is my code so far to download the file:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadClientConfigFile('client_secret.json')
drive = GoogleDrive(gauth)

team_drive_id = 'XXXXX'
parent_folder_id = 'XXXXX'
file_id = 'XXXXX'

f = drive.CreateFile({
    'id': file_id,
    'parents': [{
        'kind': 'drive#fileLink',
        'teamDriveId': team_drive_id,
        'id': parent_folder_id
    }]
})
f.GetContentFile('<file_name>')

The code returns error 404 (file not found), which makes sense: when I check the URL that GetContentFile is looking at, it is the URL leading to my drive, not the shared drive. I am probably missing a 'supportsTeamDrives': True somewhere (but where?).

There is actually a post associated to my question on https://github.com/gsuitedevs/PyDrive/issues/149 where someone raised the exact same issue. Apparently, that brought a developer to modify PyDrive about two weeks ago, but I still don't understand how to interpret his modifications and how to fix my problem. I have not noticed any other similar post on Stack Overflow (not about downloading from a shared drive anyway). Any help would be deeply appreciated.

Kind regards, Berti

Berti1989
  • 185
  • 1
  • 14

2 Answers2

1

I found an answer in a newer Github post: https://github.com/gsuitedevs/PyDrive/issues/160

Answer from SMB784 works: "supportsTeamDrives=True" should be added to files.py (pydrive package) on line 235-6.

This definitely fixed my issue.

Berti1989
  • 185
  • 1
  • 14
1

Move from pydrive to pydrive2

After encountering the same problem, I ran into this comment on an issue within the googleworkspace/PyDrive GitHub page from 2020:

...there is an actively maintained version PyDrive2 (Travis tests, regular releases including conda) from the DVC.org team...please give it a try and let us know - https://github.com/iterative/PyDrive2

Moving from the pydrive package to the pydrive2 package enabled me to download a local copy of a file stored on a shared drive by only requiring me to know the file ID.

After installing pydrive2, you can download a local copy of the file within the shared drive by using the following code template:

# load necessary modules ----
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

# authenticate yourself using your credentials
gauth = GoogleAuth()
gauth.LoadClientConfigFile('client_secret.json')
drive = GoogleDrive(gauth)

# store the file ID of the file located within the shared drive
file_id = 'XXXXX'

# store the output file name
output_file_name = 'YYYYY'

# create an instance of Google Drive file with auth of this instance
f = drive.CreateFile({'id': file_id})

# save content of this file as a local file
f.GetContentFile(output_file_name)
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33