PHP Library: Offsite File Storage System via G-Suite

This documents how to make use of the programmatic access to store info in Google Drive.

This software is currently version 0.0.1 alpha.

Design

Usually, file uploading is supposed to work in a 3-part way:

  • Stage 1: Record details about the transaction in Google Sheets (that way text is recorded easily)
  • Stage 2: Store files in an organized file structure on the server
  • Stage 3: Store files in Google Drive

File Records

To make use of the features, instantiate the class:

$fileStore = new Zysys_FileStore(client_secret_path, (optional) credentials_path);

Ensure that it is configured...this ensures that the oAuth permissions are intact with the main repository account

$fileStore->configure();

Alternatively, you can define the client_secret path and the credentials path. The client_secret key is provided by Google APIs Dashboard and it can be saved on the server (it allows the application to communicate with Google Servers). After authorizing Google to access the account, the autorization is stored in a json file located at the credentials_path.

Assign a google sheet:

$fileStore->gsheet(GOOGLE_SHEET_ID); # This id can be gathered from https://docs.google.com/spreadsheets/d/ID/edit

Identify the sheet name and the cell where rows should start. If you use A2 and Rows and Columns A2-F5 are full of data, it will add to the next available row that is empty in the column, ie A6 in our example.

$fileStore->gsheet_row("Sheet 1", "A2");

Add rows - this will instantly APPEND them to the sheet.

$fileStore->add_row(...);

To add files and add a row (which one might want to do if adding the file url to the sheet), see the example section below.

File Storage

To make use of the features, instantiate the class:

$fileStore = new Zysys_FileStore(client_secret_path, (optional) credentials_path);

Ensure that it is configured...this ensures that the oAuth permissions are intact with the main repository account

$fileStore->configure();

For ease, we recommend making use of dual configuration for local and remote storage on Google Drive.

$fileStore->local_store(path_to_local_folder);

Set the ID of the Google Drive storage folder. This ID can be gathered from https://drive.google.com/drive/u/1/folders/ID ...note make sure that the parent folder has the right permissions as they will be inherited!

$fileStore->drive_parent($FolderID);

When setting up subfolders, it's important to ensure local and remote stores are synced initially. To do that, you can use the following alias which calls create_drive_subfolder and create_local_subfolder.

$fileStore->create_subfolder("folder_name", switch_to_subfolders_bool); # To switch to the new subfolder in Drive and in Local store, set the second argument to true

$fileStore->create_local_subfolder("folder_name"); # To actually use this new folder as the new parent, see the examples section.

$fileStore->create_drive_subfolder("folder_name"); # To actually use this new folder as the new parent, see the examples section.

To actually store a file, use the following (store_file calls store_drive_file and store_local_file):

$fileStore->store_file("path_to_file"); # returns array(google drive URL, local store file path)

$fileStore->store_drive_file("path_to_file"); # returns the google drive URL

$fileStore->store_local_file("path_to_file"); # copies the file to the local file store; returns the local store file path

Example

Create a drive subdirectory and switch to it automatically

$fileStore = new Zysys_FileStore(client_secret_path, (optional) credentials_path);
$fileStore->configure();
$fileStore->drive_parent($FolderID);
$fileStore->drive_parent($fileStore->create_drive_subfolder("folder_name"));

Create a subdirectory and switch to it automatically local or drive

$fileStore = new Zysys_FileStore(client_secret_path, (optional) credentials_path);
$fileStore->configure();
$fileStore->local_store(path_to_local_folder);
$fileStore->drive_parent($FolderID);
$fileStore->drive_parent($fileStore->create_subfolder("folder_name"));
$fileStore->local_store(path_to_local_folder . "/folder_name");

Upload a file to both local and drive and have the URL entered into a sheet

$fileStore = new Zysys_FileStore(client_secret_path, (optional) credentials_path);
$fileStore->configure();
$fileStore->gsheet(GOOGLE_SHEET_ID);
$fileStore->gsheet_row("Sheet 1", "A2");
$fileSotre->local_store($localPath)
$fileStore->drive_parent($FolderID);
$fileStore->create_subfolder("folder_name", true);
#$fileStore->add_row("A2 Text", "A3 text", "Path: https://docs.google.com/document/d/" . $fileStore->store_file("path_to_file")[0] . '/view');
$fileStore->add_row("A2 Text", "A3 text", "Path: https://drive.google.com/a/googleapps_account/file/d/" . $fileStore->store_file("path_to_file")[0] . '/view');

Extra Functions

$fileStore->get_drive_parent_id(); # Returns the id of the currently active google drive folder

 
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2017-01-28 - ZachBornheimer
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback