Zayyan Masud
Zayyan Masud

Reputation: 189

How to Download a directory on Google Drive using Python?

service = self.auth()
items = self.listFilesInFolder(downLoadFolderKey)
for item in items:
    file_id = (item.get('id'))
    file_name = (item.get('name'))
    request = service.files().get_media(fileId=file_id)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print ("Download %d%%." % int(status.progress() * 100) + file_name)
    filepath = fileDownPath + file_name
    with io.open(filepath, 'wb') as f:
        fh.seek(0)
        f.write(fh.read())

I am using Google Drive API v3. I am trying to download a full directory. But the problem is the directory itself contains folders and when I try to run this bit of code. This error happens.

<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1ssF0XD8pi6oh6DXB1prIJPWKMz9dggm2?alt=media returned "Only files with binary content can be downloaded. Use Export with Google Docs files.">

The error I figure is due to it trying to download the folders, within the directory. But how do I download the full directory?

P.S The directory changes so I cannot hard code file IDs and then download the files.

Upvotes: 0

Views: 692

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your situation and goal as follows.

  • By items = self.listFilesInFolder(downLoadFolderKey), you have already been able to retrieve all file and folder list including the subfolders under the specific folder.
  • items include the mimeType for each files and folders.
  • In your issue, when the folder is used in the loop, the error occurs.
  • You want to remove this error.

For this, how about this answer?

Modification point:

  • When the mimeType is included in items of items = self.listFilesInFolder(downLoadFolderKey), the folder can be checked by the mimeType. The mimeType of folder is application/vnd.google-apps.folder.
  • From your script, I think that when the Google Docs file (Spreadsheet, Document, Slides and so on) is downloaded with the method of "Files: get", the same error occurs.
  • In order to download the Google Docs files, it is required to use the method of "Files: export".

When above point is reflected to your script, how about the following modification?

Modified script:

From:
request = service.files().get_media(fileId=file_id)
To:
file_mimeType = (item.get('mimeType'))
if file_mimeType == 'application/vnd.google-apps.folder':
    continue
request = service.files().export_media(fileId=file_id, mimeType='application/pdf') if 'application/vnd.google-apps' in file_mimeType else service.files().get_media(fileId=file_id)
  • In this modification, at first, please confirm whether the file mimeType to items of items = self.listFilesInFolder(downLoadFolderKey) is included, again. By this, the folder can be skipped and also, Google Docs files and the files except for Google Docs can be downloaded using the value of mimeType.
  • In this modification, as a sample modification, Google Docs files are downloaded as the PDF file. If you want to change the output mimeType, please modify mimeType='application/pdf'.

References:

Upvotes: 1

Related Questions