SXAPI Resource : google_drive

This resource allow you to manipulate a Google Drive Storage backend using google drive API. This resource is part of the sxapi google resource. Programmers can access resource methods and embed this module methods into there own method and endpoints. API developpers can use resource endpoints into there configuration profile to expose google data.

Based on googleapis npm module npm and is part of the sxapi-core engine from sxapi.

Resource configuration

To configure this resource, you must add a config object under the resources['google-id'].services section of your configuration profile. This key must be a drive and his value will be the configuration object who must have the appropriate configuration parameters.

For a better understanting of the sxapi configuration profile, please refer to the configuration guide

Resource config parameters

none

Example

This is a sample configuration of this resource. You must add this section under the resources section of your configuration profile

resources:
  google-id:
    _class: google
    auth: {}
    services:
      drive: {}

Resource methods

If you want to use this resource in our own module, you can retrieve this resource instance by using $app.resources.get('google-id').getService('drive') where google-id is the id of your resource as defined in the resource configuration. For more information read the getService method documentation

This module come with 20 methods.

1. findFile method 2. getFile method 3. getFileMeta method 4. copyFile method 5. moveFile method 6. addFile method 7. updateFile method 8. deleteFile method 9. exportFile method 10. getDirectory method 11. copyDirectory method 12. moveDirectory method 13. addDirectory method 14. updateDirectory method 15. deleteDirectory method 16. getPermissions method 17. addPermission method 18. updatePermission method 19. deletePermission method 20. emptyTrash method

Method findFile

Search for a list of google files matching a google query see google documentation

Parameters

Param Mandatory Type default Description
q yes string null The drive query to perform see google examples
options no object null Optional configuration for the API request.
See google Drive API files.list() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.findFile("fullText contains 'hello'", { maxResults : 50 } ,  function (error, response) {
    console.log(error, response);
});

Method getFile

Search for a google file coresponding to the id and return the document content

Parameters

Param Mandatory Type default Description
id yes string null The document ID to find
options no object null Optional configuration for the API request.
See google Drive API files.get() documentation.
response no object null Http request response. If given will be used isted of callback to steam the document content directly to the response.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions unless a response object was previously given
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data. Document data will be found in doc.Body

Example

var service = $app.resources.get('google-id').getService('drive');
// With callback
service.getFile('a4z8f5z6e85e6rt578rer5zer6z64t', { acknowledgeAbuse : false } , false, function (error, response) {
    console.log(error, response);
});
// with response (where response is and http response object)
service.getFile('a4z8f5z6e85e6rt578rer5zer6z64t', { acknowledgeAbuse : false } ,  response);

Method getFileMeta

Search for a google file coresponding to the id and return the document meta data

Parameters

Param Mandatory Type default Description
id yes string null The document ID to find
options no object null Optional configuration for the API request.
See google Drive API files.get() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.getFileMeta('a4z8f5z6e85e6rt578rer5zer6z64t', { acknowledgeAbuse : false } ,  function (error, response) {
    console.log(error, response);
});

Method copyFile

Copy a file into a new one located into another directory

Parameters

Param Mandatory Type default Description
source yes string null The file ID of the resource to copy
destination yes string null The directory ID of the destination
options no object null Optional configuration for the API request.
See google Drive API files.copy() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.copyFile( "314159265358979323846", "483514654687643543578", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method moveFile

Move a file from one directory into another directory

Parameters

Param Mandatory Type default Description
fileId yes string null The file ID of the resource to move
source yes string null The file ID of the source directory
destination yes string null The file ID of the destination directory
options no object null Optional configuration for the API request.
See google Drive API files.update() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.moveFile( "35335965465867654654",  "314159265358979323846", "483514654687643543578", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method addFile

Add a new file into a directory

Parameters

Param Mandatory Type default Description
name yes string null The file name to use in drive storage
body yes string null Document body or content
mime yes string null Mime type of this document
parent yes string null The parent directory
options no object null Optional configuration for the API request.
See google Drive API files.create() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.addFile(
    "sample file from sxapi",
    "my content", 
    "text/plain", 
    "314159265358979323846", 
    { ocr : false } ,  
    function (error, response) {
        console.log(error, response);
    });

Method updateFile

Update the content of a file

Parameters

Param Mandatory Type default Description
fileId yes string null The file ID of the resource
body yes string null Document body or content
options no object null Optional configuration for the API request.
See google Drive API files.update() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.updateFile(
    "314159265358979323846",
    "my content",  
    { ocr : false } ,  
    function (error, response) {
        console.log(error, response);
    });

Method deleteFile

Flag file as trashed

Parameters

Param Mandatory Type default Description
fileId yes string null The file ID of the resource
options no object null Optional configuration for the API request.
See google Drive API files.delete() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.deleteFile(
    "314159265358979323846"
    { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method exportFile

Export a GoogleDocs file into various mimeType

Parameters

Param Mandatory Type default Description
fileId yes string null The file ID of the template document
mime yes string null Mime type of the converted document
options no object null Optional configuration for the API request.
See google Drive API files.export() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.exportFile(
    "314159265358979323846", 
    "application/pdf", 
    { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method getDirectory

Search for a list of google files into a given directory

Parameters

Param Mandatory Type default Description
id yes string null The directory ID in google Drive
options no object null Optional configuration for the API request.
See google Drive API files.list() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.getDirectory("314159265358979323846", { maxResults : 50 } ,  function (error, response) {
    console.log(error, response);
});

Method copyDirectory

Copy a directory into a new one located into another directory

Parameters

Param Mandatory Type default Description
source yes string null The directory ID of the resource to copy
destination yes string null The directory ID of the destination
options no object null Optional configuration for the API request.
See google Drive API files.create() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.copyDirectory( "314159265358979323846", "483514654687643543578", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method moveDirectory

Move a directory from one directory into another directory

Parameters

Param Mandatory Type default Description
directoryId yes string null The file ID of the directory to move
source yes string null The file ID of the source directory
destination yes string null The file ID of the destination directory
options no object null Optional configuration for the API request.
See google Drive API files.update() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.moveDirectory( "35335965465867654654",  "314159265358979323846", "483514654687643543578", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method addDirectory

Add a new directory into another directory

Parameters

Param Mandatory Type default Description
name yes string null The directory name to use in drive storage
parent yes string null The parent directory
options no object null Optional configuration for the API request.
See google Drive API files.create() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.addDirectory(
    "sample directory from sxapi",
    "314159265358979323846", 
    { ocr : false } ,  
    function (error, response) {
        console.log(error, response);
    });

Method updateDirectory

Update the name of a directory

Parameters

Param Mandatory Type default Description
directoryId yes string null The directory ID of the resource
name yes string null New directory name
options no object null Optional configuration for the API request.
See google Drive API files.update() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.updateDirectory(
    "314159265358979323846",
    "my new name",  
    { ocr : false } ,  
    function (error, response) {
        console.log(error, response);
    });

Method deleteDirectory

Flag directory as trashed

Parameters

Param Mandatory Type default Description
directoryId yes string null The directory ID of the resource
options no object null Optional configuration for the API request.
See google Drive API files.delete() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.deleteDirectory(
    "314159265358979323846"
    { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method getPermissions

Get list of google permissions associated to a given file or directory

Parameters

Param Mandatory Type default Description
id yes string null The file or directory ID in google Drive
options no object null Optional configuration for the API request.
See google Drive API permissions.list() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.getPermissions("314159265358979323846", { } ,  function (error, response) {
    console.log(error, response);
});

Method addPermission

Add a new permission to a given file or directory

Parameters

Param Mandatory Type default Description
id yes string null The file or directory ID in google Drive
user yes string null The user name to add permission
role yes string null The user role
options no object null Optional configuration for the API request.
See google Drive API permissions.create() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.addPermission("314159265358979323846", "user@domain.org", "writer", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method updatePermission

Update one permission rule associated to a given file or directory

Parameters

Param Mandatory Type default Description
id yes string null The file or directory ID in google Drive
permId yes string null The permission ID to update
role yes string null The user new role
options no object null Optional configuration for the API request.
See google Drive API permissions.update() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.updatePermission("314159265358979323846", "2658534548766546588", "reader", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method deletePermission

Delete one permission rule associated to a given file or directory

Parameters

Param Mandatory Type default Description
id yes string null The file or directory ID in google Drive
permId yes string null The permission ID to update
options no object null Optional configuration for the API request.
See google Drive API permissions.delete() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.deletePermission("314159265358979323846", "2658534548766546588", { } ,  
    function (error, response) {
        console.log(error, response);
    });

Method emptyTrash

Empty the trash bin instantly

Parameters

Param Mandatory Type default Description
options no object null Optional configuration for the API request.
See google Drive API files.emptyTrash() documentation.
callback no function none callback function called when server answer the request.
If not defined, will throw exceptions or return the sub-resource
callback(error,response) N/A mixed null will be false or null if no error returned from google drive API. Will be a string message describing a problem if an error occur.
callback(error,response) N/A object the document meta-data

Example

var service = $app.resources.get('google-id').getService('drive');
service.emptyTrash( { } ,  function (error, response) {
    console.log(error, response);
});

Resource endpoints

This module come with one single endpoint who can interact with any google server.

1. getFile endpoint 2. addFile endpoint 3. findFile endpoint 4. listDirectory endpoint 5. addDirectory endpoint

getFile endpoint

The purpose of this endpoint is to return the content of the document directly to the http response.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "getFile"
fileId no string the Id of the file whe whan to expose.
If not given, will try to find an id key in the http request.

Example

server:
  endpoints:
  - path: "/token"
    resource: google-id
    endpoint: getFile
    fileId: '314159265358979323846'

addFile endpoint

The purpose of this endpoint is to create a new file in google Drive based on the content received from the http request.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "addFile"
parent no string The parent directory ID
If not given, will try to find an parent key in the http request.
config no object Config object to pass to the addFile method

Example

server:
  endpoints:
  - path: "/token"
    resource: google-id
    endpoint: addFile
    parent: '314159265358979323846'

findFile endpoint

The purpose of this endpoint is to find a list of file coresponding to the given query.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "findFile"
q no string The drive query to perform see google examples
If not given, will try to find an q key in the http request.

Example

server:
  endpoints:
  - path: "/token"
    resource: google-id
    endpoint: findFile
    q: fullText contains 'hello'

listDirectory endpoint

The purpose of this endpoint is to find a list of files within a given directory

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "listDirectory"
folderId no string the Id of the folder whe whan to list.
If not given, will try to find an id key in the http request.
config no object Config object to pass to the getDirectory method

Example

server:
  endpoints:
  - path: "/token"
    resource: google-id
    endpoint: listDirectory
    folderId: '314159265358979323846'

addDirectory endpoint

The purpose of this endpoint is to add a new directory into our google drive backend

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "listDirectory"
name no string the name of the folder whe want to create.
If not given, will try to find an name key in the http request.
parent no string The parent directory ID
If not given, will try to find an parent key in the http request.
config no object Config object to pass to the addDirectory method

Example

server:
  endpoints:
  - path: "/token"
    resource: google-id
    endpoint: listDirectory
    parent: '314159265358979323846'
    name: new folder from sxapi