A module that allows the user to interact with google drive's API
options
Object The options
Returns Object the instance of this class with its options and constants set
Listing files and folders
$0
Object (optional, default{}
)$0.fileId
(optional, defaultROOT_FOLDER
)$0.pageToken
(optional, defaultnull
)$0.recursive
(optional, defaultfalse
)$0.includeRemoved
(optional, defaultfalse
)$0.fields
(optional, default'nextPageToken, files(id, name, parents, mimeType, modifiedTime)'
)$0.q
(optional, default'()'
)$0.orderBy
(optional, defaultnull
)$0.spaces
(optional, default'drive'
)$0.pageSize
(optional, default100
)$0.supportsTeamDrives
(optional, defaultfalse
)$0.teamDriveId
(optional, default''
)
Downloading or exporting files
file
destinationFolder
mimeOptions
fileName
Authorizing the client
google_auth
token_path
options
files/list#request An options object (optional, default{}
)options.fileId
(string | null) The parent folder identifier,defaults to ROOT_FOLDER (optional, defaultROOT_FOLDER
)options.pageToken
(string | null) The page token when pagination is due (optional, defaultnull
)options.recursive
boolean If false, search only direct children of passed parent folder (optional, defaultfalse
)options.includeRemoved
boolean include removed files (optional, defaultfalse
)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, defaultnull
)options.spaces
string The spaces (drive, photos, appData) (optional, default'drive'
)options.pageSize
number The page size (max 1000) (optional, default100
)options.supportsTeamDrives
boolean Wether it supports team drives (optional, defaultfalse
)options.teamDriveId
string The team drive identifier (optional, default''
)
Returns Promise<files/list#response> List of files and or folders resulting from the request
- See: https://developers.google.com/drive/v3/reference/files/list
- See: https://developers.google.com/drive/v3/reference/files#resource
- See: https://developers.google.com/drive/api/v3/search-files#file_fields
parentFolder
string id of the folder from which to search. Defaults to the ROOT_FOLDER passed in the optionspageToken
string the page token of a previous request, when the prior result is paginatedrecursive
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 setincludeRemoved
boolean Either to include removed files in the listing. Defaults to falsefields
string the partial fields that should be selected
Returns Array<google.drive.files#resource> array of file resources results
- See: https://developers.google.com/drive/v3/reference/files/list
- See: https://developers.google.com/drive/v3/reference/files#resource
parentFolder
string id of the folder from which to search. Defaults to the ROOT_FOLDER passed in the optionspageToken
string the page token of a previous request, when the prior result is paginatedrecursive
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 setincludeRemoved
boolean either to list removed folders or notfields
string the partial fields that should be selected
Returns Array<google.drive.files#resource> array of folder resources results
file
google.drive.files#resource A file resource with id, name and typedestinationFolder
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 mimeTypefileName
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
file
google.drive.files#resource A file resource with id, name and typedestinationFolder
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
Create, update or delete files and folders
fileId
string The file identifier
Returns Promise<Object> retult of the deletion attempt
arg1
Object The argument 1arg1.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, defaultROOT_FOLDER
)arg1.name
string? The destination filename, defaults to the basename of the uploaded file (optional, defaultnull
)arg1.mimeType
string? The file's mime type. If not provided, Google Drive will guess it (optional, defaultnull
)arg1.fields
string? Preserved for retrocompatibility, has no effect
// 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
parentFolder
string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor optionsfolderName
string? The name of the folder that will be created
let uploadResponse = await this.create({
parentFolder:'ASDFGZXVVBBabzbdoiirrib',
name: 'new_subfolder'
});
Returns Promise<Object> the response from google drive
Deprecated methods
source
string The source file from which to read the contents of the file to uploadparentFolder
string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor optionsname
string? The destination filename, defaults to the basename of the uploaded filemimeType
string? The file's mime type. If not provided, Google Drive will guess itopts
Object An object with extra options (optional, default{}
)
// 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
oropts.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
content
string The content of the text fileparentFolder
string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor optionsdestinationFilename
string? The destination filename
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
sourcefile
string The source file from which to read the content of the PDF File to uploadparentFolder
string? The parent folder on which to write. Defaults to the ROOT_FOLDER passed in the constructor optionsdestinationFilename
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)