Search code examples
pythondownloadgoogle-drive-apiurllib2pydrive

Python: download files from google drive using url


I am trying to download files from google drive and all I have is the drive's URL.

I have read about google API that talks about some drive_service and MedioIO, which also requires some credentials( mainly JSON file/OAuth). But I am unable to get any idea about how it is working.

Also, tried urllib2.urlretrieve, but my case is to get files from the drive. Tried wget too but no use.

Tried PyDrive library. It has good upload functions to drive but no download options.

Any help will be appreciated. Thanks.


Solution

  • If by "drive's url" you mean the shareable link of a file on Google Drive, then the following might help:

    import sys
    import requests
    
    
    def download_file_from_google_drive(file_id, destination):
        URL = "https://docs.google.com/uc?export=download&confirm=1"
    
        session = requests.Session()
    
        response = session.get(URL, params={"id": file_id}, stream=True)
        token = get_confirm_token(response)
    
        if token:
            params = {"id": file_id, "confirm": token}
            response = session.get(URL, params=params, stream=True)
    
        save_response_content(response, destination)
    
    
    def get_confirm_token(response):
        for key, value in response.cookies.items():
            if key.startswith("download_warning"):
                return value
    
        return None
    
    
    def save_response_content(response, destination):
        CHUNK_SIZE = 32768
    
        with open(destination, "wb") as f:
            for chunk in response.iter_content(CHUNK_SIZE):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)
    
    
    def main():
        if len(sys.argv) >= 3:
            file_id = sys.argv[1]
            destination = sys.argv[2]
        else:
            file_id = "TAKE_ID_FROM_SHAREABLE_LINK"
            destination = "DESTINATION_FILE_ON_YOUR_DISK"
        print(f"dowload {file_id} to {destination}")
        download_file_from_google_drive(file_id, destination)
    
    
    if __name__ == "__main__":
        main()
    

    The snipped does not use pydrive, nor the Google Drive SDK, though. It uses the requests module (which is, somehow, an alternative to urllib2).

    When downloading large files from Google Drive, a single GET request is not sufficient. A second one is needed - see wget/curl large file from google drive.