Search code examples
pythonpython-3.xgoogle-drive-apigoogle-drive-shared-drive

Python: find and download missing files from google drive (using a sharable link)


Given a sharable link to a google drive folder (folder ID), I would like to compare the dir list under this folder with the dir list under a given path, and download the missing files.

I've read about PyDrive but couldn't find an elegant way to access the drive folder without authentication.

For instance:

files_under_gdrive = ["File1", "File2", "File3"]
files_under_given_path = ["File1", "some_other_file"]

# Download missing files found only in Google Drive
...

files_under_given_path = ["File1", "some_other_file", "File2", "File3"]

Any hint/idea would be highly appreciated. Thanks :)


Solution

  • You could easily start by gathering the files from the local directory and store their names in a list for example.

    Afterwards, in order to retrieve the files from the Google Drive Shared drive, you can make use of Files.list request:

    GET https://www.googleapis.com/drive/v3/files
    

    With the includeItemsFromAllDrives set to true and the driveId field set to the corresponding id of the shared drive. Depending on your exact needs and requirements, you may add other fields to the request as well.

    After retrieving the files from the shared Google Drive, you can simply compare the two lists and based on the results, download the needed files. For downloading the files, you may want to check this snippet from the Drive API documentation:

    file_id = 'ID_OF_THE_FILE_TO_DOWNLOAD'
    request = drive_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)
    

    However, I recommend you to start by completing the Google Drive Quick Start with Python from here.

    Reference