mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-04 19:21:53 +00:00
c96159268e
* feat google storage * feat google storage * add google storage writablestream * add google storage writablestream * add google storage writablestream * add metadata to google storage * add metadata to google storage * add metadata to google storage * add tags to google storage * fix * fix * fix * fix
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
const { Storage } = require('@google-cloud/storage');
|
|
const { Writable } = require('stream');
|
|
|
|
class GoogleStorageUploadStream extends Writable {
|
|
|
|
constructor(logger, opts) {
|
|
super(opts);
|
|
this.logger = logger;
|
|
this.metadata = opts.metadata;
|
|
|
|
const storage = new Storage(opts.bucketCredential);
|
|
this.gcsFile = storage.bucket(opts.bucketName).file(opts.Key);
|
|
this.writeStream = this.gcsFile.createWriteStream();
|
|
|
|
this.writeStream.on('error', (err) => this.logger.error(err));
|
|
this.writeStream.on('finish', () => {
|
|
this.logger.info('google storage Upload completed.');
|
|
this._addMetadata();
|
|
});
|
|
}
|
|
|
|
_write(chunk, encoding, callback) {
|
|
this.writeStream.write(chunk, encoding, callback);
|
|
}
|
|
|
|
_final(callback) {
|
|
this.writeStream.end();
|
|
this.writeStream.once('finish', callback);
|
|
}
|
|
|
|
async _addMetadata() {
|
|
try {
|
|
await this.gcsFile.setMetadata({metadata: this.metadata});
|
|
this.logger.info('Google storage Upload and metadata setting completed.');
|
|
} catch (err) {
|
|
this.logger.error(err, 'Google storage An error occurred while setting metadata');
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = GoogleStorageUploadStream;
|