Save Any File to Google Drive

I have been working on doing this for quite some time within AppInventor, but the tools required on board are just not quite there to do it yet. However, I have come up with a “slingshot” solution using a combination of web technologies that will get a file from your device up to google drive.

In order to achieve this, you will require the following:

  1. A google web app
  2. An https web server with php and ftp access
  3. A couple of php scripts / pages
  4. Ai2 developed app

Credits to Scott Ferguson for the postfile howto, Digital Inspiration for the bones of the apps script, and to Taifun for the File extension 🙂

Again I assume that you have a good grip of AI2, writing and publishing google app script web apps, and the ability to ftp to your web space on an https server (Google Drive will NOT accept urls from non https servers)

 

The workflow goes something like this:

  1. In the app, select a file, including the full path
  2. Once we have this, upload the file to the https server directory
  3. On completion of the upload, trigger the google web app, by sending the url to the file on the web server to it
  4. Once the web app has uploaded the file, delete the file from the web server

On the app:

I used Taifun’s File extension to access a list of files from the sdcard, based upon extension – gif,jpg,png,pdf – this list can be extended, or developers can simply use * to get all files.

It was then simply a case of working through three web calls to post the file, upload to drive, then delete the initial posted file for clean up purposes.

Blocks

 

 

 

 

Setting up the php and web server

FTP into your https server web space and create a directory, in my case called “AI2”. Inside that directory, create a directory called “uploads” or something similar. I set permissions for “uploads” to 777, not sure if this was necessary. Upload a file using FTP and identify the url (if you use an image file it will be easier to view this in a browser). You will need this url later. Should be something like:

https://<domain>/AI2/uploads/image.png

Now we create to simple php files, one which will save the uploaded file to the https server space, the other that will delete it.

phpupload.php

phpdelete.php

 

Note: the delete function can be very dangerous, use with caution and do not release the full url to anyone else. There is a failsafe included which should restrict any deletions to the upfiles directory. Given the plan is to remove any files once they have been transferred to google drive, means that in practice this shouldn’t be a problem…

Upload these two files to your AI2 directory using FTP

 

The Google Web App

The web app requires to be fed with the url, discovered earlier, of the file uploaded. This is constructed on the AI2 app and sent using the web component. On successful completion, the web app returns happiness to the AI2 app, which then sets about deleting the original uploaded file from the https server

The google app script:

The AI2 demo app for this, on screen, is fairly uninspiring, a listpicker to fetch the file, an upload button, and success is demonstrated by the return of the filename

Video

 

Enjoy 🙂

4 thoughts on “Save Any File to Google Drive”

    1. If you have been following the example:

      Your two php files will be in a directory called AI2
      The $base_directory is the subfolder “uploads” where the files are actually placed

      So the line should read:

      $base_directory ='uploads/';

      This method ensures that only files in that directory can be deleted. It is important that you keep the url and the name of the directory private, as much as possible.

      This method is not recommended for serious production scenarios, but is suitable for this example

Leave a Reply

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