fox-send/server/storage/gcs.js

43 lines
854 B
JavaScript
Raw Permalink Normal View History

2018-10-05 18:01:58 +00:00
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) {
2018-11-16 21:33:40 +00:00
return this.bucket.file(id).createReadStream({ validation: false });
2018-10-05 18:01:58 +00:00
}
set(id, file) {
return new Promise((resolve, reject) => {
file
.pipe(
this.bucket.file(id).createWriteStream({
validation: false,
2019-08-07 17:10:42 +00:00
resumable: true
})
)
2018-10-05 18:01:58 +00:00
.on('error', reject)
.on('finish', resolve);
});
}
del(id) {
return this.bucket.file(id).delete();
}
ping() {
return this.bucket.exists();
}
}
module.exports = GCSStorage;