Skip to content

Commit

Permalink
add google storage writablestream
Browse files Browse the repository at this point in the history
  • Loading branch information
xquanluu committed Jul 28, 2023
1 parent 1372f8f commit cb483a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
30 changes: 28 additions & 2 deletions lib/record/google-storage.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
async function upload(logger, socket) {}
module.exports = upload;
const { Storage } = require('@google-cloud/storage');
const { Writable } = require('stream');

class GoogleStorageUploadStream extends Writable {

constructor(logger, opts) {
super(opts);
this.logger = logger;

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) => console.error(err));
this.writeStream.on('finish', () => console.log('Upload completed.'));
}

_write(chunk, encoding, callback) {
this.writeStream.write(chunk, encoding, callback);
}

_final(callback) {
this.writeStream.end();
this.writeStream.once('finish', callback);
}
}

module.exports = GoogleStorageUploadStream;
17 changes: 2 additions & 15 deletions lib/record/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@

const path = require('node:path');
async function record(logger, socket, url) {
const p = path.basename(url);
const idx = p.lastIndexOf('/');
const vendor = p.substring(idx + 1);
switch (vendor) {
case 'aws_s3':
return require('./s3')(logger, socket);
case 'google':
return require('./google-storage')(logger, socket);
default:
logger.info(`unknown bucket vendor: ${vendor}`);
socket.send(`unknown bucket vendor: ${vendor}`);
socket.close();
}
async function record(logger, socket) {
return require('./upload')(logger, socket);
}

module.exports = record;
16 changes: 2 additions & 14 deletions lib/record/s3.js → lib/record/upload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Account = require('../models/account');
const Websocket = require('ws');
const PCMToMP3Encoder = require('./encoder');
const S3MultipartUploadStream = require('./s3-multipart-upload-stream');
const wav = require('wav');
const { getUploader } = require('./utils');

async function upload(logger, socket) {

Expand Down Expand Up @@ -43,19 +43,7 @@ async function upload(logger, socket) {
Key += `/${day.getDate().toString().padStart(2, '0')}/${callSid}.${account[0].record_format}`;

// Uploader
const uploaderOpts = {
bucketName: obj.name,
Key,
metadata,
bucketCredential: {
credentials: {
accessKeyId: obj.access_key_id,
secretAccessKey: obj.secret_access_key,
},
region: obj.region || 'us-east-1'
}
};
const uploadStream = new S3MultipartUploadStream(logger, uploaderOpts);
const uploadStream = getUploader(Key, metadata, obj);

/**encoder */
let encoder;
Expand Down

0 comments on commit cb483a7

Please sign in to comment.