Skip to content

linx-software/google-drive-file-management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

google-drive-file-management

Description

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.

Installation

Register Google API service acccount credentials:

  1. Register a new project/app on the Google Developer console.
  2. Enable the following APIs for your project.
    • Select the scopes:
      • Google Drive API
      • Google Sheets API
  3. Create new 'Service Account' credentials with owner permissions.
  4. Click Manage service accounts
  5. In the Actions column, click Create Key.
  6. Export the credentials as a JSON file.
  7. Copy the whole JSON object from the exported file.

Configure your Solution:

  1. Install Linx Designer. Download it here.

  2. Open the sample Solution (.lsoz) in your Linx Designer.

  3. 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.

  4. Save the Solution.


Managing your file items

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.


Getting the details of authenticated user

Retrieve the details of the authenticated service account.

  1. Debug or drag the GetAbout function in your own function.
  2. Run the function.
  3. View the details of the authenticated service account as the result of the web service call.

Generate and add custom properties to files

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.

Search for files by custom property

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.


Delete files and log a record entry

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.

Download non-Google file types to your local drive

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.


Download GoogleDrive file types to your local drive

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.

Upload files to GoogleDrive

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:

  1. Place files in the source directory.
  2. Debug or drag the UploadLocalFilesToGDrive function in your own function.
  3. Run the function.
  4. Your files will be uploaded to the GoogleDrive API and the local file is moved to a backup folder on your local drive.

Upload files to GoogleDrive (Google file types)

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.

  1. Place files in the source directory.
  2. Debug or drag the UploadLocalFilesToGDriveInGoogleFormat function in your own function.
  3. Run the function.
  4. Your files will be uploaded to the GoogleDrive API and the local file is moved to a backup folder on your local drive.

Missing pieces

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.

Contributing

For questions please ask the Linx community or use the Slack channel.

License

MIT

Releases

No releases published

Packages

No packages published