added gcs

This commit is contained in:
Danny Coates 2018-11-02 14:24:10 -07:00
parent 9bb36cd827
commit 53426b950a
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
5 changed files with 4318 additions and 6074 deletions

37
server/storage/gcs.js Normal file
View file

@ -0,0 +1,37 @@
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
class GCSStorage {
constructor(config, log) {
this.bucket = storage.bucket(config.gcs_bucket);
this.log = log;
}
async length(id) {
const data = await this.bucket.file(id).getMetadata();
return data[0].size;
}
getStream(id) {
return this.bucket.file(id).createReadStream();
}
set(id, file) {
return new Promise((resolve, reject) => {
file
.pipe(this.bucket.file(id).createWriteStream())
.on('error', reject)
.on('finish', resolve);
});
}
del(id) {
return this.bucket.file(id).delete();
}
ping() {
return this.bucket.exists();
}
}
module.exports = GCSStorage;