Skip to content

Latest commit

 

History

History
346 lines (279 loc) · 13.5 KB

API.md

File metadata and controls

346 lines (279 loc) · 13.5 KB

NodeGoogleDrive

A module that allows the user to interact with google drive's API


Parameters

Returns Object the instance of this class with its options and constants set


list

Listing files and folders


Parameters

  • $0 Object (optional, default {})
    • $0.fileId (optional, default ROOT_FOLDER)
    • $0.pageToken (optional, default null)
    • $0.recursive (optional, default false)
    • $0.includeRemoved (optional, default false)
    • $0.fields (optional, default 'nextPageToken, files(id, name, parents, mimeType, modifiedTime)')
    • $0.q (optional, default '()')
    • $0.orderBy (optional, default null)
    • $0.spaces (optional, default 'drive')
    • $0.pageSize (optional, default 100)
    • $0.supportsTeamDrives (optional, default false)
    • $0.teamDriveId (optional, default '')

exportFile

Downloading or exporting files


Parameters

  • file
  • destinationFolder
  • mimeOptions
  • fileName

getNewOauthToken

Authorizing the client


Parameters

  • google_auth
  • token_path

list


Parameters

  • options files/list#request An options object (optional, default {})
    • options.fileId (string | null) The parent folder identifier,defaults to ROOT_FOLDER (optional, default ROOT_FOLDER)
    • options.pageToken (string | null) The page token when pagination is due (optional, default null)
    • options.recursive boolean If false, search only direct children of passed parent folder (optional, default false)
    • options.includeRemoved boolean include removed files (optional, default false)
    • options.fields string fields to include in the request The fields (optional, default 'nextPageToken,files(id, name, parents, mimeType, modifiedTime)')
    • options.q files/list#search-parameters query string to filter results. (optional, default '()')
    • options.orderBy string Optinally sort results by a given field (optional, default null)
    • options.spaces string The spaces (drive, photos, appData) (optional, default 'drive')
    • options.pageSize number The page size (max 1000) (optional, default 100)
    • options.supportsTeamDrives boolean Wether it supports team drives (optional, default false)
    • options.teamDriveId string The team drive identifier (optional, default '')

Returns Promise<files/list#response> List of files and or folders resulting from the request


listFiles


Parameters

  • parentFolder string id of the folder from which to search. Defaults to the ROOT_FOLDER passed in the options
  • pageToken string the page token of a previous request, when the prior result is paginated
  • recursive string wether to list also files in subfolders of the requested parentFolder. defaults to true. If false, omits the files under subfolders. Works only when parentFolder is explicitly set
  • includeRemoved boolean Either to include removed files in the listing. Defaults to false
  • fields string the partial fields that should be selected

Returns Array<google.drive.files#resource> array of file resources results


listFolders


Parameters

  • parentFolder string id of the folder from which to search. Defaults to the ROOT_FOLDER passed in the options
  • pageToken string the page token of a previous request, when the prior result is paginated
  • recursive string wether to list also files in subfolders of the requested parentFolder. defaults to true. If false, omits the files under subfolders. Works only when parentFolder is explicitly set
  • includeRemoved boolean either to list removed folders or not
  • fields string the partial fields that should be selected

Returns Array<google.drive.files#resource> array of folder resources results


exportFile


Parameters

  • file google.drive.files#resource A file resource with id, name and type
  • destinationFolder string The destination folder to download to (use absolute paths to avoid surprises)
  • mimeOptions Object An object containing the extension and mimetype of the desired export format. If not set, it will take the default according to the file mimeType
  • fileName String The file name without extension (the extension must be passed in the mimeOptions argument) Defaults to the file resource's name

Returns Promise A promise that resolves when the file is downloaded


getFile


Parameters

  • file google.drive.files#resource A file resource with id, name and type
  • destinationFolder string The destination folder to download to (use absolute paths to avoid surprises)
  • fileName string (optional) The file name. Defaults to the file resource's name

Returns Promise A promise that resolves when the file is downloaded


createOrDelete

Create, update or delete files and folders


removeFile


Parameters

  • fileId string The file identifier

Returns Promise<Object> retult of the deletion attempt


create


Parameters

  • arg1 Object The argument 1
    • arg1.source string The path to a local file, a {@ReadStream} or content (plain or binary) to upload (optional, default 'some file')
    • arg1.parentFolder string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor options (optional, default ROOT_FOLDER)
    • arg1.name string? The destination filename, defaults to the basename of the uploaded file (optional, default null)
    • arg1.mimeType string? The file's mime type. If not provided, Google Drive will guess it (optional, default null)
    • arg1.fields string? Preserved for retrocompatibility, has no effect

Examples

//  Create a text file sending the contents as a string
 let uploadResponse = await gdriveInstance.create({
   source: 'THIS WILL BE THE CONTENT OF MY FILE',
   parentFolder: 'ASDFGHZXCCVVFVEVEW',
   name: 'hello_world.txt'
   mimeType: 'text/plain'
 });
//create a Google Spreadsheet from a local CSV File
 let transformResponse = await gdriveInstance.create({
   source:'./data/XCODE_mini.csv',
   name: 'XCODE Spreadsheet',
   parentFolder: 'ASDFGHZXCCVVFVEVEW',
   mimeType: 'application/vnd.google-apps.spreadsheet'
 });
// Stream a PDF document to Google Drive
 let uploadResponse = await gdriveInstance.create({
   source:fs.createReadStream('./data/sample.pdf'),
   name: 'MyDocument.pdf',
   parentFolder: 'ASDFGHZXCCVVFVEVEW',
   mimeType: 'application/pdf'
 });
// Create a subfolder

 let folderCreation = await gdriveInstance.create({
   parentFolder: null, // <--- this will create the subfolder below the root folder
   name: 'Generic Folder',
   mimeType: 'application/vnd.google-apps.folder'
 });

Returns Promise<Object> the response from google drive


createFolder


Parameters

  • parentFolder string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor options
  • folderName string? The name of the folder that will be created

Examples

let uploadResponse = await this.create({
   parentFolder:'ASDFGZXVVBBabzbdoiirrib',
   name: 'new_subfolder'
 });

Returns Promise<Object> the response from google drive


Deprecated

Deprecated methods


writeFile


Parameters

  • source string The source file from which to read the contents of the file to upload
  • parentFolder string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor options
  • name string? The destination filename, defaults to the basename of the uploaded file
  • mimeType string? The file's mime type. If not provided, Google Drive will guess it
  • opts Object An object with extra options (optional, default {})
    • opts.destinationMimeType string? Takes precedence over the mimeType parameter
    • opts.fields string? Fields to ask in the request to Google Drive

Examples

// create a Google Spreadsheet from a local CSV File
 let transformResponse = await gdriveInstance.writeFile(
   './data/XCODE_mini.csv',
   null,
   'XCODE Spreadsheet',
   null,
   {
     destinationMimeType: 'application/vnd.google-apps.spreadsheet' <--- convert to this format
   }
 );

Returns Promise<Object> the response from google drive

Meta

  • deprecated: : use create instead Writes a file to Google Drive. Delegates on method create.

    If mimeType or opts.destinationMimeType aren't set, Google will detect the file type if possible. Set this explicitly to convert common files to native google docs/sheets/slides, etc


writeTextFile


Parameters

  • content string The content of the text file
  • parentFolder string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor options
  • destinationFilename string? The destination filename

Examples

let uploadResponse = await gdriveInstance.writeTextFile(
   'THIS WILL BE THE CONTENT OF MY FILE',
   'ASDFGHZXCCVVFVEVEW',
   'hello_world.txt'
 );

Returns Promise<Object> the response from google drive

Meta

  • deprecated: : use create instead Shorthand method to create a text file. Kept for retrocompatibility

writePDFFile


Parameters

  • sourcefile string The source file from which to read the content of the PDF File to upload
  • parentFolder string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor options
  • destinationFilename string? The destination filename

Returns Promise<Object> the response from google drive

Meta

  • deprecated: : use create instead Just an example method to show how to upload a PDF (You should be using create directly, instead)