-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstorage.js
44 lines (43 loc) · 1.12 KB
/
storage.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
const { Storage } = require("@google-cloud/storage");
const GCS_BUCKET = "amae-koromo";
class DataStorage {
constructor () {
const storage = new Storage();
const bucket = storage.bucket(GCS_BUCKET);
this._bucket = bucket;
}
async get (name, defaultValue = undefined) {
let resp;
try {
resp = await this._bucket.file(name).download();
} catch (e) {
return defaultValue;
}
return JSON.parse(resp[0].toString("utf8"));
}
async getRaw (name, defaultValue = undefined) {
let resp;
try {
resp = await this._bucket.file(name).download();
} catch (e) {
return defaultValue;
}
return resp[0];
}
async set (name, value) {
return await this._bucket.file(name).save(Buffer.from(JSON.stringify(value), "utf8"), {
resumable: false,
contentType: "application/json",
metadata: {
cacheControl: "public, max-age=60",
},
});
}
async setRaw (name, value) {
return await this._bucket.file(name).save(value, {
resumable: false,
contentType: "application/octet-stream",
});
}
}
exports.DataStorage = DataStorage;