PyDrive icon indicating copy to clipboard operation
PyDrive copied to clipboard

Downloading files from Team Drive

Open dtlloyd opened this issue 6 years ago • 8 comments

Hi,

I have managed to access the file list in a folder in a Team Drive using:

file_list = drive.ListFile( { 'q': "'YYY' in parents and trashed=false", 'corpora': "teamDrive", 'teamDriveId': "XXX", 'includeTeamDriveItems': "true", 'supportsTeamDrives': "true" }).GetList() for file1 in file_list: print('title: %s, id: %s' % (file1['title'], file1['id']))

I think this was helped by creating a "settings.yaml" file in the working directory in which the python code lives. See: the documentation for more detail.

I have also been able to upload a simple, text file to the same team drive:

team_drive_id = 'XXX' parent_folder_id = 'YYY'

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

where XXX and YYY replace the specific ids in question.

However, I am not able to download a copy of a file from the team drive folder to my local directory:

file = drive.CreateFile({'id': file_list[0]['id']}) file.GetContentFile(file_list[0]['title'],'.bmp')

returns a 404 error.

Is there something going on with the permissions, such that I can read file names, upload but not download? Or is there an error in the syntax I have used for downloading the file contents?

Any tips or suggestions would be welcome.

Thanks, David

dtlloyd avatar Mar 08 '19 11:03 dtlloyd

I'm also experiencing this problem and was wondering if this feature is even implemented. ListFile() and Upload() accept optional param arguments that allow a supportsTeamDrives option, but it seems like GetContentFile() does not accept any optional param argument and does not give us the ability to set a supportsTeamDrives option.

DomDiPasquale5 avatar Apr 09 '19 16:04 DomDiPasquale5

Same here...

jonatascastro12 avatar Apr 22 '19 14:04 jonatascastro12

I am also having the same problem. The ListFile() method works, but then I get 404 file not found errors when I attempt to either invoke GetPermissions() or GetContentFile() for any files from my team drive. I am directly inputting the entire file info into the CreateFile() function and it still gives a 404 error. Here is my code:

####################################################################### file_URL = "https://drive.google.com/open?id=myFileIDgoesHereNormally"

file_id = file_URL.split("id=")[1]

file_list = drive.ListFile({'q': '',
'corpora': 'teamDrive',
'teamDriveId': 'myTeamDriveIDgoesHereNormally',
'includeTeamDriveItems': True,
'supportsTeamDrives': 'true'}).GetList()

for file in file_list: if(file['id']==file_id): drive_file=drive.CreateFile(file) print(drive_file.GetPermissions()) drive_file.GetContentFile(Path(download_dir/file['title'])) ########################################################################

Both drive_file.GetPermissions() and drive_file.GetContentFile() produce a 404 error, which leads me to believe that drive.CreateFile() is failing for some reason.

SMB784 avatar Jun 06 '19 16:06 SMB784

I have discovered the problem.

The issue was that when yehcj created his pull request and his branch was merged with master (found here: https://github.com/yehcj/PyDrive/commit/a90b874afa50ec14762975ec7232981d3b7a9100), the following line was not added to files.py in the master branch:

@@ -235,6 +241,7 @@ def FetchMetadata(self, fields=None, fetch_all=False): metadata = self.auth.service.files().get( fileId=file_id, fields=fields, # Teamdrive support supportsTeamDrives=True ).execute(http=self.http) except errors.HttpError as error:

The master branch files.py is missing "supportsTeamDrives=True". If you add that back into the files.py on your local system at the location described above, the files can successfully be downloaded from TeamDrives (I have verified this). I have not checked to see if the rest of the changes in that branch have been successfully merged with master, but at least the download is working.

SMB784 avatar Jun 07 '19 15:06 SMB784

This issue appears to have been addressed in the latest commit, as the supportsTeamDrives=True line has been added to the master branch. Therefore I suggest that this issue be closed.

SMB784 avatar Jun 27 '19 23:06 SMB784

Doesn't work for me (GetContentFile). Maybe it's because supportsTeamDrives has been deprecated, and it's called supportsAllDrives now?

askemottelson avatar Sep 29 '19 18:09 askemottelson

I can confirm that replacing all instances of supportsTeamDrives with supportsAllDrives and includeTeamDriveItems with includeItemsFromAllDrives in PyDrive/pydrive/files.py indeed fixes the issue

askemottelson avatar Sep 30 '19 08:09 askemottelson

Works for me too! I have just had to add the supportsAllDrives=True at line 235 of files.py, like so:

metadata = self.auth.service.files().get(fileId=file_id, fields=fields, supportsAllDrives=True).execute(http=self.http)

hellomanel avatar Feb 05 '21 17:02 hellomanel