-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive_csv_upload.js
98 lines (90 loc) · 3.04 KB
/
drive_csv_upload.js
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
// Copyright 2024 CyberWeb Consulting LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Obtain a copy of the License at: apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// FILE: drive_csv_upload.js
// POST: dev.to/googleworkspace/import-csv-to-google-sheets-without-the-sheets-api-20g1
const fs = require('fs').promises;
const path = require('path');
const process = require('process');
const {Readable} = require('stream');
const {authenticate} = require('@google-cloud/local-auth');
const {google} = require('googleapis');
const CREDENTIALS_PATH = path.join(process.cwd(), 'client_secret.json');
const TOKEN_STORE_PATH = path.join(process.cwd(), 'storage.json');
const SCOPES = ['https://www.googleapis.com/auth/drive.file'];
const FILENAME = 'inventory.csv';
const METADATA = {name: FILENAME};
/**
* Load any saved credentials or null
*
* @return {(Promise<JSONClient|null>)}
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_STORE_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
/**
* Save credentials
*
* @param {JSONClient} client credentials
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
access_token: client.credentials.access_token,
token_expiry: client.credentials.token_expiry,
scopes: client.credentials.scopes,
});
await fs.writeFile(TOKEN_STORE_PATH, payload);
}
/**
* Authorize user credentials, authenticating user first if necessary
*
* @return {JSONClient} client credentials
*/
async function authorize() {
var client = await loadSavedCredentialsIfExist();
if (client) return client;
client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) await saveCredentials(client);
return client;
}
/**
* Upload CSV file as-is with Drive API
*
* @param {JSONClient} authClient (client credentials)
*/
async function uploadCSV(authClient) {
const drive = google.drive({version: 'v3', auth: authClient});
const data = await fs.readFile(FILENAME);
const res = await drive.files.create({
requestBody: METADATA,
media: {body: Readable.from(data)}
});
console.log(`** Uploaded '${FILENAME}' to Drive (file ID: ${res.data.id})`);
}
authorize().then(uploadCSV).catch(console.error);