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 its working.
Also, tried urllib2 urlretrieve, but my case is to get files from 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.
If by "drive's url" you mean the shareable link of a file on Google Drive, then the following might help:
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.
Having had similar needs many times, I made an extra simple class
GoogleDriveDownloader
starting on the snippet from @user115202 above. You can find the source code here.You can also install it through pip:
Then usage is as simple as:
This snippet will download an archive shared in Google Drive. In this case
1iytA1n2z4go3uVCwE__vIKouTKyIDjEq
is the id of the sharable link got from Google Drive.The above function downloads the file given the file_id to a specified downloads folder. Now the question remains, how to get the file_id? Simply split the url by id= to get the file_id.
This has also been described above,
This creates its own server too do the dirty work of authenticating
This downloads the file
PyDrive
allows you to download a file with the functionGetContentFile()
. You can find the function's documentation here.See example below:
This code assumes that you have an authenticated
drive
object, the docs on this can be found here and here.In the general case this is done like so:
Info on silent authentication on a server can be found here and involves writing a
settings.yaml
(example: here) in which you save the authentication details.