5
votes

I have a folder on the google drive with just .jpg images and I want to download all of the images in the folder to my computer using the folder's shared link.

So far, the only thing that I have found that works is the code below but I can only get it to work for specific shared files and not an entire folder.

from google_drive_downloader import GoogleDriveDownloader as gdd

gdd.download_file_from_google_drive(file_id='1viW3guJTZuEFcx1-ivCL2aypDNLckEMh',
                                    dest_path='./data/mnist.zip',
                                    unzip=True)

Is there a way to modify this to work with google folders or is there another way to download google drive folders?

1
I think your question is answered here : stackoverflow.com/questions/38511444/…aseem bhartiya
@aseembhartiya I've tried that too but it only works with specific files, I need to download the whole folder.Brandalf
Can I ask you about your situation? You want to download all files in the shared folder. If my understanding is correct, are you required to use python? For example, there is a CLI tool for downloading all files from the shared folder. How about this? If I misunderstood your question, I apologize.Tanaike
@Tanaike I just need some way for python ran program to download files from a shared google drive folder. It is going to be run on a Raspberry PiBrandalf
@Brandalf Thank you for replying. I'm not sure whether this CLI tool can be used for your situation, how about this? youtube.com/watch?v=ncYzrwTGaJATanaike

1 Answers

2
votes

If you use the Python Quickstart Tutorial for Google Drive API which you can find here, you'll be able to set up your authentication with the API. You can then loop through your Drive and only download jpg files by specifying the MIMEType of your search to be image/jpeg.

First, you want to loop through the files on your drive using files: list:

# Note: folder_id can be parsed from the shared link
def listFiles(service, folder_id):
    listOfFiles = []

    query = f"'{folder_id}' in parents and mimeType='image/jpeg'"

    # Get list of jpg files in shared folder
    page_token = None
    while True:
        response = service.files().list(
            q=query,
            fields="nextPageToken, files(id, name)",
            pageToken=page_token,
            includeItemsFromAllDrives=True, 
            supportsAllDrives=True
        ).execute()

        for file in response.get('files', []):
            listOfFiles.append(file)

        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    return listOfFiles

Then you can download them by using the files: get_media() method:

import io
from googleapiclient.http import MediaIoBaseDownload

def downloadFiles(service, listOfFiles):
    # Download all jpegs
    for fileID in listOfFiles:
        request = service.files().get_media(fileId=fileID['id'])
        fh = io.FileIO(fileID['name'], 'wb')
        downloader = MediaIoBaseDownload(fh, request)

        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Downloading..." + str(fileID['name']))

You can refine your search further by using the q parameter in listFiles() as specified here.