@robinpath/google-drive
0.1.1Node.jsPublicGoogle Drive module for RobinPath.
Google Drive
Google Drive module for RobinPath.
Package: @robinpath/google-drive | Category: Cloud Storage | Type: Integration
Authentication
googleDrive.setCredentials "ya29.xxx"
Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.
Use Cases
Use the google-drive module when you need to:
- List files in Google Drive with optional query filter. -- Use
google-drive.listFilesto perform this operation - Get file metadata by ID. -- Use
google-drive.getFileto perform this operation - Download file content as text. -- Use
google-drive.downloadFileto perform this operation - Upload a file to Google Drive. -- Use
google-drive.uploadFileto perform this operation - Create a new folder in Google Drive. -- Use
google-drive.createFolderto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
setCredentials | Set the OAuth2 access token for Google Drive API. | Confirmation message. |
listFiles | List files in Google Drive with optional query filter. | Object with files array and nextPageToken. |
getFile | Get file metadata by ID. | File metadata object. |
downloadFile | Download file content as text. | File content as text. |
uploadFile | Upload a file to Google Drive. | Uploaded file metadata. |
createFolder | Create a new folder in Google Drive. | Created folder metadata. |
deleteFile | Permanently delete a file or folder. | Confirmation message. |
moveFile | Move a file to a different folder. | Updated file metadata. |
copyFile | Copy a file, optionally with a new name or destination. | Copied file metadata. |
shareFile | Share a file with a user by email. | Permission object. |
Functions
setCredentials
Set the OAuth2 access token for Google Drive API.
Module: google-drive | Returns: string -- Confirmation message.
googleDrive.setCredentials "ya29.xxx"
| Parameter | Type | Required | Description |
|---|---|---|---|
accessToken | string | Yes | OAuth2 access token |
listFiles
List files in Google Drive with optional query filter.
Module: google-drive | Returns: object -- Object with files array and nextPageToken.
googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
| Parameter | Type | Required | Description |
|---|---|---|---|
options | object | No | Options: q (query), pageSize, pageToken, orderBy |
getFile
Get file metadata by ID.
Module: google-drive | Returns: object -- File metadata object.
googleDrive.getFile "file-id"
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file ID |
downloadFile
Download file content as text.
Module: google-drive | Returns: string -- File content as text.
googleDrive.downloadFile "file-id"
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file ID to download |
uploadFile
Upload a file to Google Drive.
Module: google-drive | Returns: object -- Uploaded file metadata.
googleDrive.uploadFile "report.txt" "Hello world" {"folderId":"folder-id"}
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | File name |
content | string | Yes | File content |
options | object | No | Options: mimeType, folderId |
createFolder
Create a new folder in Google Drive.
Module: google-drive | Returns: object -- Created folder metadata.
googleDrive.createFolder "My Folder"
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Folder name |
parentId | string | No | Parent folder ID (optional) |
deleteFile
Permanently delete a file or folder.
Module: google-drive | Returns: string -- Confirmation message.
googleDrive.deleteFile "file-id"
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file/folder ID to delete |
moveFile
Move a file to a different folder.
Module: google-drive | Returns: object -- Updated file metadata.
googleDrive.moveFile "file-id" "folder-id"
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file ID to move |
newFolderId | string | Yes | Destination folder ID |
copyFile
Copy a file, optionally with a new name or destination.
Module: google-drive | Returns: object -- Copied file metadata.
googleDrive.copyFile "file-id" {"name":"Copy of Report"}
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file ID to copy |
options | object | No | Options: name, folderId |
shareFile
Share a file with a user by email.
Module: google-drive | Returns: object -- Permission object.
googleDrive.shareFile "file-id" "user@example.com" "writer"
| Parameter | Type | Required | Description |
|---|---|---|---|
fileId | string | Yes | The file ID to share |
email | string | Yes | Email of the user to share with |
role | string | No | Permission role: reader, writer, commenter (default: reader) |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
Google Drive: token not configured. Call googleDrive.setCredentials first. | Check the error message for details |
Google Drive API error (${res.status}): ${text} | Check the error message for details |
googleDrive.setCredentials requires an access token. | Check the error message for details |
googleDrive.getFile requires a fileId. | Check the error message for details |
googleDrive.downloadFile requires a fileId. | Check the error message for details |
Google Drive download error (${res.status}): ${text} | Check the error message for details |
googleDrive.uploadFile requires name and content. | Check the error message for details |
Google Drive upload error (${res.status}): ${text} | Check the error message for details |
@desc "List files and validate result"
do
set $result as googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
if $result != null
print "Success"
else
print "No result"
end
enddo
Recipes
1. List and iterate Files
Retrieve all items and loop through them.
@desc "List files and iterate results"
do
google-drive.setCredentials $token
set $result as googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
each $item in $result
print $item
end
enddo
2. Create a new item with createFolder
Create a new resource and capture the result.
@desc "Create folder"
do
google-drive.setCredentials $token
set $result as googleDrive.createFolder "My Folder"
print "Created: " + $result
enddo
3. Check before creating
List existing items and only create if needed.
@desc "List files and create folder"
do
google-drive.setCredentials $token
set $existing as googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
if $existing == null
googleDrive.createFolder "My Folder"
print "Item created"
else
print "Item already exists"
end
enddo
4. Multi-step Google Drive workflow
Chain multiple google-drive operations together.
@desc "List files, get file, and more"
do
google-drive.setCredentials $token
set $r_listFiles as googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
set $r_getFile as googleDrive.getFile "file-id"
set $r_downloadFile as googleDrive.downloadFile "file-id"
print "All operations complete"
enddo
5. Safe listFiles with validation
Check results before proceeding.
@desc "List files and validate result"
do
google-drive.setCredentials $token
set $result as googleDrive.listFiles {"q":"mimeType='application/pdf'","pageSize":10}
if $result != null
print "Success: " + $result
else
print "Operation returned no data"
end
enddo
Related Modules
- s3 -- Amazon S3 module for complementary functionality
- dropbox -- Dropbox module for complementary functionality
- box -- Box module for complementary functionality
- onedrive -- OneDrive module for complementary functionality
- json -- JSON module for complementary functionality
Versions (1)
| Version | Tag | Published |
|---|---|---|
| 0.1.1 | latest | 1 months ago |
Related Modules
github
JS@robinpathv0.1.1
GitHub module for RobinPath.
bitbucket
JS@robinpathv0.1.2
Bitbucket module for RobinPath.
box
JS@robinpathv0.1.2
Box module for RobinPath.
cache
JS@robinpathv0.1.2
In-memory key-value cache with optional TTL expiration for temporary data storage
$ robinpath add @robinpath/google-drive
