-
Notifications
You must be signed in to change notification settings - Fork 54
/
datatransfer.js
68 lines (55 loc) · 1.5 KB
/
datatransfer.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
const {Storage} = require('@google-cloud/storage');
const shouldUpload = process.env.mode == "demo";
const shouldDownload = process.env.mode == "run";
process.env.GOOGLE_APPLICATION_CREDENTIALS = "auth/bucketAuth.json"
async function downloadFile(
bucketName = 'showroom-database',
srcFilename = 'testdb.h2.db',
destFilename = require('os').homedir() + '/testdb.h2.db'
) {
const storage = new Storage();
const options = {
destination: destFilename,
validation: "md5"
};
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
}
async function uploadFile (
bucketName = 'showroom-database',
srcFilename = '/root/testdb.h2.db',
destFilename = 'testdb.h2.db'
) {
const storage = new Storage();
const options = {
gzip: true,
validation: "md5",
metadata: {
cacheControl: 'public, max-age=31536000',
}
};
await storage
.bucket(bucketName)
.upload(srcFilename, options);
console.log(
`${srcFilename} uploaded to gs://${bucketName}/${destFilename}.`
);
}
async function uploadToDatabase() {
console.log("upload database");
uploadFile().catch((err) => console.log(err));
}
async function downloadFromDatabase() {
console.log("download database");
downloadFile().catch((err) => console.log(err));
}
if(shouldUpload){
uploadToDatabase();
} else if (shouldDownload) {
downloadFromDatabase();
}