How to download a big file from google drive via curl in Bash?

Viewed 3213

I wanna make a very simple bash script for downloading files from google drive via Drive API, so in this case there is a big file on google drive and I installed OAuth 2.0 Playground on my google drive account, then in the Select the Scope box, I choose Drive API v3, and https://www.googleapis.com/auth/drive.readonly to make a token and link.

After clicking Authorize APIs and then Exchange authorization code for tokens. I copied the Access tokenlike below.

#! /bin/bash

read -p 'Enter your id : ' id
read -p 'Enter your new token : ' token
read -p 'Enter your file name : ' file

curl -H "Authorization: Bearer $token" "https://www.googleapis.com/drive/v3/files/$id?alt=media" -o "$file"

but it won't work, any idea ?

for example the size of my file is 12G, when I run the code I will get this as output and after a second it back to prompt again ! I checked it in two computers with two different ip addresses.(I also add alt=media to URL)

-bash-3.2# bash mycode.sh

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   166  100   166    0     0     80      0  0:00:02  0:00:02 --:--:--    80

-bash-3.2# 

the content of file that it created is like this

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "downloadQuotaExceeded",
    "message": "The download quota for this file has been exceeded."
   }
  ],
  "code": 403,
  "message": "The download quota for this file has been exceeded."
 }
}
3 Answers
  • You want to download a file from Google Drive using the curl command with the access token.

If my understanding is correct, how about this modification?

Modified curl command:

Please add the query parameter of alt=media.

curl -H "Authorization: Bearer $token" "https://www.googleapis.com/drive/v3/files/$id?alt=media" -o "$file"

Note:

  • This modified curl command supposes that your access token can be used for downloading the file.
  • In this modification, the files except for Google Docs can be downloaded. If you want to download the Google Docs, please use the Files: export method of Drive API. Ref

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

UPDATE AS FOR MARCH 2021

Simply follow this guide here. It worked for me.

In summary:

For small files to download run

wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O FILENAME

While if you are trying to download a quite large file you should try to run

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FILEID" -O FILENAME && rm -rf /tmp/cookies.txt

Simply substitute FILEID and FILENAME with your custom values.

FILEID can be found in your file share link (after the /d/ as illustrated in the article mantioned above).

FILENAME is simply the name you want to save the download as. Remember to include the right extension. For Example FILENAME = my_file.pdf if the file is a pdf.

This is a known bug

It has been reported in this Issue Tracker post. This is caused because as you can read in the documentation:

(about download url)

Short lived download URL for the file. This field is only populated for files with content stored in Google Drive; it is not populated for Google Docs or shortcut files.

So you should use another field.


You can follow the report by clicking on the star next to the issue number to give more priority to the bug and to receive updates.


As you can read in the comments of the report, the current workaround is:

  • Use webContentlink instead

or

  • Change www.googleapis.com to content.googleapis.com
Related