Managing the difference instances of your file storage such as syncing between your local file system and GoogleDrive can become time consuming and repetative. Using the GoogleDrive API and the Linx GoogleDrive Plugin, you are able to automate many of the file operations between your local drive and a your Google Drive instance.
Using this sample you are able to:
- Upload, download and remove files from a local file system to a Google Drive instance.
- Manage meta data and translation of GoogleDrive items.
Register Google API service acccount credentials:
- Register a new project/app on the Google Developer console.
- Enable the following APIs for your project.
- Select the scopes:
- Google Drive API
- Google Sheets API
- Select the scopes:
- Create new 'Service Account' credentials with owner permissions.
- Click Manage service accounts
- In the Actions column, click Create Key.
- Export the credentials as a JSON file.
- Copy the whole JSON object from the exported file.
Configure your Solution:
-
Install Linx Designer. Download it here.
-
Open the sample Solution (.lsoz) in your Linx Designer.
-
Edit the $.Settings values:
Name Type Value GoogleAPIAuthCredentials
string {copied json object} ShareWithUserEmail
string {your email} ShareWithUserEmail
: This will be used to share items between the service account that was registered and your personal google drive account so that you may see the files in your GoogleDrive dashboard. -
Save the Solution.
The below sections describe the different functions that can be used to sync items on your local drive with GoogleDrive. These are standalone pieces of functionality which can be run individually in your Linx Designer or automated with a service.
Retrieve the details of the authenticated service account.
- Debug or drag the GetAbout function in your own function.
- Run the function.
- View the details of the authenticated service account as the result of the web service call.
In some cases, custom properties can be used to translate files between systems.
This process generates random Ids for files and then writes them out to a file. This file can then be used for record keeping purposes or linking to other systems. If you want to add your own custom property value then just modify the process to take in your value instead of the random string.
So for instance if you had a file stored on Sharepoint with ID 'XXX' , you could add a custom property 'XXX' to the matching file on GoogleDrive. This will allow you to match the files together on the different systems.
The process works as follows:
- All the file items linked to the service account are retrieved from the API.
- For each file, a random string is generated and then the file is updated with this new custom property.
- The file details are then updated on the GDrive API.
- A call is then made to retrieve the updated object from the API and then written to a local text file.
Once custom properties are added to file items, you'll want to search for them. This process allows you to import a file of custom property values and search for matching file items.
The process works as follows:
- The list of drive item ids that was generated by 'GenerateAndAddCustomProperties' is read.
- For each line, the file details of the matching item on the API are retrieved.
This process doesnt achieve anything more, use it as a template for looping through GDrive files and searching.
Add your logic where the 'doSomething' placeholder is if you want to execute functionality with the matching file details.
Deleting files is often neccessary (especially when developing automations with the API), and you may want to remove files after they have been sent off or are unused. The process imports a file with custom property values, it searches for matching files on the GoogleDrive API and then deletes them off the server.
The process works as follows:
- The list of drive item ids that was generated by 'GenerateAndAddCustomProperties' is read.
- For each line, the drive item matching the ID is deleted from the API.
- A log is then written of the deleted file details.
When working with different file systems such as Microsoft and Google, there is a difference in file types which affects what you can do with them. In the case of the GoogleDrive API, there is a differnce between native google MIME types and other commonly used MIME types. In order to export non-google file types i.e. .docx, .xlsx from a GoogleDrive instance different logic needs to be used to that of native google items.
This function downloads the contents of non-native Google file types and writes them out to a local folder in the Office MIME type.
The GoogleDrive API provides a special endpoint for exporting native google drive items. However, these files need to be written to a local file system and most likely read by human eyes. In these cases, you need to convert the GoogleDrive MIME type, into the native mime type (i.e. commonly used MIME types).
So for example, if I had a application/vnd.google-apps.document
(Google Doc) , I need to convert that to a .docx
(Microsoft Word) if I want to view it in office. To get around this I had to build a custom translation function which would allow me to write out the data returned into the needed file type.
The process works as follows:
- A list of the native google drive items are retrieved from the API.
- Each file's mime type is then passed into a custom translation functions which returns the Office version file extension i.e. '.docx'.
- A request is then made to the GDrive API to export the file data.
- The file data is then written to a local file in the translated file format.
This function uploads the contents of a local file directory to a Google drive instance in their native format. This process imports files from a directory, searches for a matching file on the GDrive API and adds/updates the file in the original format.
To sync your files:
- Place files in the source directory.
- Debug or drag the UploadLocalFilesToGDrive function in your own function.
- Run the function.
- Your files will be uploaded to the GoogleDrive API and the local file is moved to a backup folder on your local drive.
This function uploads the contents of a local file directory to a Google drive instance in their matching native Google format. This process works by converting the MIME type into the Google format if needed. The file data is then sent to the API if the file activity on the local drive is more recent.
- Place files in the source directory.
- Debug or drag the UploadLocalFilesToGDriveInGoogleFormat function in your own function.
- Run the function.
- Your files will be uploaded to the GoogleDrive API and the local file is moved to a backup folder on your local drive.
As the sample stands, these are independant functions that have to be run manually by debugging. However, these could be easily automated by linking them up to a timer service or file drop event.
For questions please ask the Linx community or use the Slack channel.