6

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?

Brandalf
  • 476
  • 1
  • 6
  • 20
  • I think your question is answered here : https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url – aseem bhartiya Jul 02 '19 at 18:10
  • @aseembhartiya I've tried that too but it only works with specific files, I need to download the whole folder. – Brandalf Jul 02 '19 at 18:44
  • 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 Jul 02 '19 at 23:02
  • @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 Pi – Brandalf Jul 03 '19 at 21:00
  • @Brandalf Thank you for replying. I'm not sure whether this CLI tool can be used for your situation, how about this? https://www.youtube.com/watch?v=ncYzrwTGaJA – Tanaike Jul 03 '19 at 22:50

2 Answers2

4

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.

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • 1
    Thanks for the suggestion but the program I am trying to make must use only a link from a shared google drive folder. – Brandalf Jul 03 '19 at 21:05
  • I've updated my answer to include parameters for searching a specific shared Drive in `Files: list()`. Make sure to change `'SHARED_DRIVE_ID'` to the ID of your shared Drive. – Rafa Guillermo Jul 04 '19 at 08:12
0

You can use the gdrive tool. Basically it is a tool to access a google drive account from command-line. Follow this example for a Linux machine to set this up:

  1. At first download the execution file of gdrive here.
  2. Decompress it and get the executable file gdrive. Now change the permissions of the file by executing the command chmod +x gdrive.
  3. Run ./gdrive about and you will get a URL asking you to enter for a verification code. As instructed in the prompt copy the link and go to the URL on your browser, then log into your Google Drive account and grant permission. You will get some verification code at last. Copy it.
  4. Go back to your previous terminal and paste the verification code you just copied. Then verify your information there. Now you successfully link your machine to your Google Drive account.

Now once done with the above process you can navigate the files on your drive using the commands mentioned below.

./gdrive list # List all files' information in your account
./gdrive list -q "name contains 'University'" # serch files by name
./gdrive download fileID # Download some file. You can find the fileID from the 'gdrive list' result.
./gdrive upload filename  #  Upload a local file to your google drive account.
./gdrive mkdir # Create new folder

Hopefully, this helps.

Gaurav Shrivastava
  • 905
  • 12
  • 19