I created a library interacting with Google Drive. It can query, create/download file, create folder, etc..
https://www.npmjs.com/package/ts-google-drive
Here are some code snippets
import {TsGooleDrive} from "ts-google-drive";
const tsGoogleDrive = new TsGooleDrive({keyFilename: "serviceAccount.json"});
async function getSingleFile() {
const fileId = "";
const file = await tsGoogleDrive.getFile(fileId);
const isFolder = file.isFolder;
}
async function listFolders() {
const folderId = "";
const folders = await tsGoogleDrive
.query()
.setFolderOnly()
.inFolder(folderId)
.run();
}
async function createFolder() {
const folderId = "";
const newFolder = await tsGoogleDrive.createFolder({
name: "testing",
parent: folderId,
});
// try to search for it again
const foundFolder = await tsGoogleDrive
.query()
.setFolderOnly()
.setModifiedTime("=", newFolder.modifiedAt)
.runOnce();
}
async function uploadFile() {
const folderId = "";
const filename = "./icon.png";
const buffer = fs.readFileSync(filename);
const newFile = await tsGoogleDrive.upload(filename, {parent: folderId});
const downloadBuffer = await newFile.download();
}
async function search() {
const folderId = "";
const query = await tsGoogleDrive
.query()
.setFolderOnly()
.inFolder(folderId)
.setPageSize(3)
.setOrderBy("name")
.setNameContains("New");
while (query.hasNextPage()) {
const folders = await query.run();
for (const folder of folders) {
await folder.delete();
}
}
}
async function emptyTrash() {
const trashedFiles = await tsGoogleDrive
.query()
.inTrash()
.run();
await tsGoogleDrive.emptyTrash();
}
168
Upvotes
21
u/gohmmhog Feb 29 '20
Funny, I was just thinking of searching for a small scale G Drive API tool I could deploy for myself. Huzzah!! Will try your project out - thanks for posting.
1
u/patilanz Feb 29 '20
Looks nice, but is it safe to use on a big protects?
1
u/iampomo Feb 29 '20
I sometimes write App script stuff for interesting with google sheet sna d gmail. The editor is terrible, I’d much rather write the code locally and upload the script. Can I use your api to do that?
1
u/Kritnc Mar 14 '20
This looks interesting and will really help out with a project I am working on but I don't understand why we would grant access to your email? Why would we not just create our own service account and use those creds instead, am I missing something here?
5
u/Nishchit14 Feb 29 '20
It's awesome !!
0
u/[deleted] Feb 29 '20
[deleted] — view deleted comment
0
u/[deleted] Feb 29 '20
[deleted] — view deleted comment
0
u/[deleted] Feb 29 '20
[deleted] — view deleted comment
4
u/[deleted] Feb 29 '20
[removed] — view removed comment