Skip to content

Instantly share code, notes, and snippets.

@Mukei
Forked from tanaikech/submit.md
Created June 13, 2022 14:27
Show Gist options
  • Save Mukei/3e1787a8361d1a44ffdd4a838728cece to your computer and use it in GitHub Desktop.
Save Mukei/3e1787a8361d1a44ffdd4a838728cece to your computer and use it in GitHub Desktop.
Downloading Shared Files on Google Drive Using Curl

Downloading Shared Files on Google Drive Using Curl

When the shared files on Google Drive is downloaded, it is necessary to change the download method by the file size. The boundary of file size when the method is changed is about 40MB.

File size < 40MB

CURL

filename="### filename ###"
fileid="### file ID ###"
curl -L -o ${filename} "https://drive.google.com/uc?export=download&id=${fileid}"

File size > 40MB

When it tries to download the file with more than 40MB, Google says to download from following URL.

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

Query included confirm=#### is important for downloading the files with large size. In order to retrieve the query from the HTML, it uses pup.

pup 'a#uc-download-link attr{href}'

And sed is used to remove amp;.

sed -e 's/amp;//g'`

So curl command is as follows.

CURL

filename="### filename ###"
fileid="### file ID ###"
query=`curl -c ./cookie.txt -s -L "https://drive.google.com/uc?export=download&id=${fileid}" | pup 'a#uc-download-link attr{href}' | sed -e 's/amp;//g'`
curl -b ./cookie.txt -L -o ${filename} "https://drive.google.com${query}"

The value of confirm is changed every time. So when the value of confirm is retrieved, the cookie is saved using -c ./cookie.txt, and when the file is downloaded, the cookie has to be read using -b ./cookie.txt.

By using this method, you can download files with the size of over 1 GB.

Reference: http://qiita.com/netwing/items/f2a595389f39206235e8

Update at March 21, 2019

sample script

This is a simple bash 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}

Reference: https://stackoverflow.com/questions/48133080/how-to-download-a-google-drive-url-via-curl-or-wget/48133859#48133859

CLI tool

If you will use a CLI tool, please use this. This is a CLI tool to download shared files and folders from Google Drive. For large file, the resumable download can be also run.

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
    
  • Also, goodls was updated to v1.2.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment