-
Notifications
You must be signed in to change notification settings - Fork 7
/
.sample.ts
102 lines (86 loc) · 2.83 KB
/
.sample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as fs from "fs";
import {google} from "googleapis";
import {TsGoogleDrive} from "./src";
const tsGoogleDrive = new TsGoogleDrive({keyFilename: "serviceAccount.json"});
async function auth() {
const drive1 = new TsGoogleDrive({keyFilename: "serviceAccount.json"});
const drive2 = new TsGoogleDrive({credentials: {client_email: "", private_key: ""}});
// for steps in getting access_token using oauth, you can take reference below
// https://medium.com/@terence410/using-google-oauth-to-access-its-api-nodejs-b2678ade776f
const drive3 = new TsGoogleDrive({oAuthCredentials: {access_token: ""}});
const drive4 = new TsGoogleDrive({oAuthCredentials: {refresh_token: ""}, oauthClientOptions: {clientId: "", clientSecret: ""}});
}
async function getSingleFile() {
const fileId = "";
const file = await tsGoogleDrive.getFile(fileId);
if (file) {
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 uploadAndDownload() {
const folderId = "";
const filename = "./icon.png";
const newFile = await tsGoogleDrive.upload(filename, {parent: folderId});
const downloadBuffer = await newFile.download();
// of if you want stream
const drive = google.drive({version: "v3", auth: newFile.client});
const file = await drive.files.get({
fileId: newFile.id,
alt: 'media'
}, {responseType: "stream"});
file.data.on("data", data => {
// stream data
});
file.data.on("end", () => {
// stream end
});
// or use pipe
const writeStream = fs.createWriteStream('./output.png');
file.data.pipe(writeStream);
}
async function search() {
const folderId = "";
const query = await tsGoogleDrive
.query()
.setFolderOnly()
.inFolder(folderId)
.setPageSize(3)
.setOrderBy("name")
.setNameContains("New");
// or you can use any query https://developers.google.com/drive/api/v3/search-files
query.setQuery("name = 'hello'");
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();
}