Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a Google Drive url via curl or wget

Is there a way to download a publicly-viewable Google Drive url via curl or wget? For example, being able to do something like:

curl -O myfile.xls https://drive.google.com/uc?export=download&id=1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl 

Note, I'm looking to do this on a publicly-viewable file without having to sign into my Google account (or have someone else sign into their account, etc.).

If helpful, the cors headers I have are:

 "kind": "drive#file",  "id": "1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl", 
like image 582
David542 Avatar asked Jan 06 '18 23:01

David542


People also ask

How do I download from Google Drive using curl?

Right-click on the file you want to download, click share, under link sharing section, select "anyone with this link can edit". Copy the FILEIDENTIFIER portion from the link. Copy the below script to a file. It uses curl and processes the cookie to automate the downloading of the file.

How do I download a URL using curl?

Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz. You can set the output file name while downloading file with the curl, execute: $ curl -o file. pdf https://your-domain-name/long-file-name.pdf.


1 Answers

How about this method? When the file is such large size, Google returns a code for downloading the file. You can download the file using the code. When such large file is downloaded using curl, you can see the code as follows.

<a id="uc-download-link" class="goog-inline-block jfk-button jfk-button-action" href="/uc?export=download&amp;confirm=ABCD&amp;id=### file ID ###">download</a> 

The query with confirm=ABCD is important for downloading the file. This code is also included in the cookie. At the cookie, you can see it as follows.

#HttpOnly_.drive.google.com TRUE    /uc TRUE    #####   download_warning_#####  ABCD 

In this case, "ABCD" is the code. In order to retrieve the code from the cookie and download the file, you can use the following script.

Sample script :

#!/bin/bash fileid="### file id ###" filename="MyFile.csv" curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename} 

If this was not useful for you, I'm sorry.

Updated at February 17, 2022

Recently, it seems that the specification of this flow has been changed. So I updated this answer. In order to download a publicly shared file of large size from Google Drive, you can use the following script.

#!/bin/bash fileid="### file id ###" filename="MyFile.csv" html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}"` curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Po '(confirm=[a-zA-Z0-9\-_]+)'`&id=${fileid}" -o ${filename} 
  • In this case, the ID for downloading is retrieved from the HTML data as follows.

      <form id="downloadForm" action="https://drive.google.com/uc?export=download&amp;id={fileId}&amp;confirm={value for downloading}" method="post"> 
  • When you want to download a publicly shared file of small size from Google Drive, you can use the following command.

      curl -L "https://drive.google.com/uc?export=download&id=### fileId ###" -o sampleoutput.csv 
like image 148
Tanaike Avatar answered Sep 28 '22 03:09

Tanaike