Purpose
This module allows a Lua script to authenticate and access Google Drive. It allows browsing and managing the files and folders on Google Drive.Dependencies
Installation
Either copy the gdrive.lua file in a directory in the Lua package.path or use LuaRocks to install it like:Basic Usage
The demo.lua file gets access to google drive and then prints out the response to the request to get the root directory listing in the drive.To make the example code work below follow these steps to get the credentials file from Google API for your script:
- Log in to the google developer console.
- Go to the Drive API link under the Google Apps APIs
- Enable this API and download a credentials file
- From the credentials file take the 'installed' json table and paste in a new file creds.json and place the file in the same directory as this demo file
- Now execute the code
Example code from demo.lua in the test subdirectory:
API Reference
Module
API
The gdrive module exposes just 1 function:new
To create a new gdrive object.Syntax:
Usage:
Inputs:
config table to configure the new gdrive object. The configuration table can have all the same keys as required by the oauth2 module config input but all are optional except the credential keys:
- creds_file: [OPTIONAL] Full path and name for the credentials file. This parameter if not given then the credentials must be provided in the creds table described below.
- creds: [OPTIONAL] Table containing the connection credentials. This table must have the following keys:
- client_id: String key for the client id.
- client_secret: String key for the client secret.
- redirect_uris: [OPTIONAL] Redirect URIs provided by the service
- auth_uri or auth_url: [OPTIONAL] URL string for requesting authorization. NOTE: If this is given it will overwrite the auth_uri or auth_url given in the main config table
- token_uri or token_url: [OPTIONAL] URL string for requesting token. NOTE: If this is given it will overwrite the token_uri or token_url given in the main config table
The other parameters used by default in the configuration, which are overwritten if provided in the input table, are:
Returns:
- nil and the error message in case of failure to create object
- The gdrive connection object
gdrive object
API
item
To check if an item exist on the drive and to get the item object to access and manipulate the item.Syntax:
Usage:
Inputs:
- gdrive object. This can be the calling gdrive object using the colon operator
- name - This is the name of the object with the full path from the root that we want to check or obtain the object of.
"/folder1/folder1a"
,"\folder1\folder1a"
,"folder1/folder1a"
,"folder1\folder1a"
are all valid names of the same folder1a object inside folder1 in root. - type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error
- false if such a item does not exist
- item object for the file or folder item if success
mkdir
To make a directory hierarchy on the drive and get its object for accessing and manipulating the directory. If the folder already exist then it will return that folder's object and not create anything.Syntax:
Usage:
Inputs:
- gdrive object. This can be the calling gdrive object using the colon operator
- directory name - This is the name of the folder with the full path from the root that we want to create.
"/folder1/folder1a/folder1aa"
,"\folder1\folder1a\folder1aa"
,"folder1/folder1a/folder1aa"
,"folder1\folder1a\folder1aa"
are all valid names of the same folder1aa object inside folder1a in folder1 in root. - type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error
- folder object if success
Objects
config
This table contains all the configuration parameters of the oAuth 2.0 object. This table structure is the same as the config structure described in the config object input to the new function.mimeType
This table contains definitions of types of items that can reside on the Google drive. The table below shows its members:This can be used to test the type of item object you have like:
tokenUpdated
This is a boolean flag. It is set to true whenever the access token is refreshed by the oAuth 2.0 protocol. If you want to detect when the token is updated set this to false and then poll it periodically or after each request to see if the token was updated. Note the token can be accessed by accessinggd.oauth2.token
root
This is an item object for the root folder for the Google drive. See the item object documentation below for more information on item objects.oauth2
This is an oauth2 object used for the connection by the gdrive object. For more information on this object look hereacquireToken
This is a table array containing 2 elements:- The URL string to access to get the access code from the Google Drive service so that the access token can be generated.
- The function to be called with the access code as the parameter to generate the access token and setup the connection
gd.root
object exists. If it does not then go ahead and acquire the token.item Object
API
getProperty
Gets the value of any property name for the item objectSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- property name - The property name can be any string from the list of data available for an item. The full list can be seen here
- nil and a error message if error
- property object which may be a string or a table depending on the property on success
setProperty
Sets the value of any property name for an item objectSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- property name - The property name can be any string from the list of data available for an item. The full list can be seen here
- value: This may either be a string or a table depending on the property whose value is being set
- nil and a error message if error
- true if success setting the property
list
Gets the list of items inside a folder item. This function should only be called for a folder item otherwise it will return nilSyntax:
Usage:
Inputs:
- item object. This can be the calling item object using the colon operator
- number of entries on page - This tells the number of entries on the dirList object page. Default is 100
- nil and a error message if error
- dirList object with all the item objects in the 1st page of the listing on success.
item
To check if an item exist in the folder and to get the item object to access and manipulate the item. This should be called for folder items otherwise it will return a nil.Syntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- name - This is the name of the object that we want to retrieve or check
- type - This is a string specifying whether we are specifying a 'file' or a 'folder'. Default is 'file'
- nil and a error message if error
- false if such a item does not exist
- item object for the file or folder item if success
mkdir
To make a directory in the folder and get its object for accessing and manipulating it. If the folder already exist then it will return that folder's object and not create anything.Syntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- directory name - This is the name of the folder that we want to create.
- nil and a error message if error
- folder object if success
upload
Function to upload file to the Google drive folderSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- file name - This is the name of the file that is being uploaded.
- source - is a function which on each call returns the next chunk of data
- force - It will not upload if the file already exists unless force is true
- nil and a error message if error
- file object of the uploaded file if success
move
Function to move an item from current parents to the given parent objectSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- destination object - This is the destination folder object
- force - It will not upload if the file already exists unless force is true
- nil and a error message if error
- true if success
rename
Function to rename an item to a new nameSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- new name - This is the new name for the item
- force - It will not rename if another item with the same name exists in that folder unless force is true. In which case the old item is deleted.
- nil and a error message if error
- true if success
copyto
Function to copy an item from current parents to the given parent objectSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- destination object - This is the destination folder object
- force - It will not copy if the file with same name already exists in the destination folder unless force is true
- nil and a error message if error
- copied item object if success
delete
Function to delete an item from the Google DriveSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- nil and a error message if error
- true if success
download
Function to download a file or a portion of itSyntax:
Usage:
Inputs:
- item object. This can be the calling folder item object using the colon operator
- sink function - This is a function which is called and passed the file data to be used.
- starting byte - The starting byte of the file data from where to start downloading
- stopping byte - The stopping byte of the file data uptill where to download
- nil and a error message if error
- the return value of the sink function after passing data to it.
dirList Object
This object allows browsing a directory listing from Google DriveAPI
next
This function gets the next listing page if all the results did not fit in this pageSyntax:
Usage:
Inputs:
- dirList object. This should be the calling directory list object using the colon operator
- nil and a error message if error
- directory list object for the next page