My *nix world

Transparently download from Google Drive

Google Drive is a file storage and synchronization service provided by Google which enables user cloud storage, file sharing and collaborative editing.

The storage limit for the free accounts is, at the time of writing, about 5GB.

If you have a blog, like I have, and sometimes you write an article and you want to share a files rather than inserting its contents into the articles page, you could use your free Google account and the Google Drive service to store and share that file.

The problem

The problem that I have encountered regarding this matter was that, when sharing a file you get a public share link, like the one below:


If you provide this address as your HTML link reference then you will not be prompted by the "Save as" window, instead the Google Drive Viewer application will be shown and this will allow you, of course, to download that file:
transparently download from Google Drive

a file shown in Google Drive Viewer

As we can see in the link provided above, it is structured as following:

  • the secure connection protocol : https
  • the (sub)domain name : docs.google.com
  • other application/directories within the Google website : file/d/
  • the file unique identifier : 0B95k2kr1bG9feGVhd0p3QkVCSVE
  • the action/command to execute about that file: view (or edit, etc)

If I would write an article and I wish to insert a link to a shared file (stored on Google Drive) I would not be happy to let my readers/visitors to open the Google Drive Viewer then to put them to click one more time, and so on. What I would like to achieve would be to get directly that Save as windows:

click to zoom

the "Save As" window when clicking on a file link

Obviously there are (at least) two types of downloads where you really need the direct download link:

  1. nice to have: on a link inserted on a HTML page (such as a wordpress.com article)
  2. must to have: on a link where you are supposed to download the file programmatically

In the first case you could live even without the direct link, maybe the Google Drive Viewer it's not such a big deal at the end.

In the second case, if you don't have a direct link, you could end by downloading a content which is not your file but the Google Drive Viewer html content for the requested file:

My solution(s)

Obviously, by trial an error, I found two solutions to transparently download from Google Drive:

  1. For the link you would insert into your web page (such as a wordpress.com article) you could use the following pattern:
  • https://docs.google.com/uc?export=download&confirm=no_antivirus&id=file-id

Ex: https://docs.google.com/uc?export=download&id=0B95k2kr1bG9feGVhd0p3QkVCSVE

The above method works if and only if the file is shared at least for the "who has the link".
  1. For the link where you are supposed to download the file programmatically the pattern above will not work, instead you will get an error message like below:

I've found that the following pattern will work like a charm:
  • https://googledrive.com/host/file-id

Ex: https://googledrive.com/host/0B95k2kr1bG9feGVhd0p3QkVCSVE

Note that in the second case (the one above) the file name you will be prompted to save (programmatically or not) will not be the original shared file name (like in the first example) but the file hashed identifier (such as 0B95k2kr1bG9feGVhd0p3QkVCSVE). Since this method provides a direct link (by the file-id) to download the file programmatically (eg: wget -O URL), it would not be a challenge for any programmer to choose whatever name he/she likes for the output-file.

The above method works if and only if the file is shared publicly (i.e. it's not enough to be shared to "who has the link").

How to obtain the file-id for the Google Drive file

Open your Google Drive, right-click on your file and then choose Share option. It will open a window like the one below:

google-drive-file-id

The file-id you are looking for is right on the field "Link to share". It is always enclosed between the https://drive.google.com/file/d/ and /edit? parts (eg. 0B95k2kr1bG9faXdzaWNIOGNmSU0).

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Transparently download from Google Drive

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Transparently download from Google Drive

Latest posts by Eugen Mihailescu (see all)

20 thoughts on “Transparently download from Google Drive

  1. t

    Thanks for this man, really saved my bacon. Wish google would just tell us how to do these stuff so we don't have to have all the pain finding out little hacks like this, and then google can just change it whenever they want and just break it...

    well anyway thanks man, also confirming that it still works now.

    1. Eugen Mihailescu

      If you will try the sample download link that I have mentioned in this article you will see that they are still working. Perhaps you have tested your own links (files) and they are either not shared or you have used a wrong file-id. I have no idea what is wrong with your test.

      1. sandro

        It was because the file was not downloadable anymore due to the BW limits, Google fixed this bug. Before this method bypassed it that's why it suddenly stopped working

  2. cicevicek

    I think this works as long as the file "does not exceed the maximum size that Google can scan". For larger files this method gets the html page with that info.

    1. Eugen Mihailescu

      Hi Nathan!

      I think that the file-id you've mentioned (14Tjjz_bIQOZywNIXcQfGSJUZVl0LWOj94PZfKsvbHs0) it's the problem. Check again how did you get that file-id, obviously it's not that it should be.
      Check out again My Solution(s) section above. It shows you exactly what you have to do. I actually double-checked that those links on "My solutions" section are (still) working as described and they do.

    1. Eugen Mihailescu

      Oh, I've just downloaded yours .conf file with exactly the same command and it works like a charm!
      So the problem is not the command itself but your proxy, your Internet connection, wget version that you might have installed, etc.
      Anyway thanks for sharing your feedback.

      1. Gigi Duru

        You are right, just tried on ubuntu and it works but I need it on my router with entware.
        Thanks for pointing me to the right direction

  3. Chris Trude

    Commenting over from your other blog. So turns out you can't DL the whole folder in it's entirety, but the good news is that it turns out I was trying to unzip the gz the wrong way. Figured it out and am good to go. Thanks for chatting!

  4. Alessio

    Hi, it seems to me that if you want to access it programmatically, you need to set sharing options (on Drive side) to "everyone could find and access".
    With "everyone with link" , it doesn't work.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: