Please note that this blog has been moved.
Now it has its own domain: mynixworld.info 🙂
If you want to read the latest version of this article (recommended) please click here and I open the page for you.
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 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:
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:
Obviously there are (at least) two types of downloads where you really need the direct download link:
- nice to have: on a link inserted on a HTML page (such as a wordpress.com article)
- 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 downloading a content which is not your file but the Google Drive Viewer html content for the requested file:
<!DOCTYPE html><html><head><meta name="google" content="notranslate"> <meta http-equiv="X-UA-Compatible" content="IE=edge;"> <meta name="fragment" content="!"><title>.config.rpi $ c&&0<b&&(c.tick("_tbnd",void 0,window.chrome.csi().startE), c.tick("tbnd_","_tbnd",b))),null==a&&window.gtbExternal&&(a=window ....
Obviously, by trial an error, I found solutions for both cases.
- For the link you would insert into your web page (such as a wordpress.com article) you could use the following pattern:
- 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:
root@rpi-gentoo ~ $ --2013-02-23 10:33:32-- https://docs.google.com/uc?export=download Resolving docs.google.com... 18.104.22.168, 22.214.171.124, 126.96.36.199, ... Connecting to docs.google.com|188.8.131.52|:443... connected. HTTP request sent, awaiting response... 500 Internal Server Error 2013-02-23 10:33:32 ERROR 500: Internal Server Error.
I’ve found that the following pattern will work like a charm:
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 <output-file> URL), it would not be a challenge for any programmer to choose whatever name he/she likes for the output-file.