mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-23 04:21:56 +00:00
9d24ef6238
* azure storage * azure uploader * azure uploader * azure uploader * fix
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
const { Writable } = require('stream');
|
|
const { BlobServiceClient } = require('@azure/storage-blob');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
class AzureStorageUploadStream extends Writable {
|
|
constructor(logger, opts) {
|
|
super(opts);
|
|
const blobServiceClient = BlobServiceClient.fromConnectionString(opts.connection_string);
|
|
this.blockBlobClient = blobServiceClient.getContainerClient(opts.bucketName).getBlockBlobClient(opts.Key);
|
|
this.metadata = opts.metadata;
|
|
this.blocks = [];
|
|
}
|
|
|
|
async _write(chunk, encoding, callback) {
|
|
const blockID = uuidv4().replace(/-/g, '');
|
|
this.blocks.push(blockID);
|
|
try {
|
|
await this.blockBlobClient.stageBlock(blockID, chunk, chunk.length);
|
|
callback();
|
|
} catch (error) {
|
|
callback(error);
|
|
}
|
|
}
|
|
|
|
async _final(callback) {
|
|
try {
|
|
await this.blockBlobClient.commitBlockList(this.blocks);
|
|
// remove all null/undefined props
|
|
const filteredObj = Object.entries(this.metadata).reduce((acc, [key, val]) => {
|
|
if (val !== undefined && val !== null) acc[key] = val;
|
|
return acc;
|
|
}, {});
|
|
await this.blockBlobClient.setMetadata(filteredObj);
|
|
callback();
|
|
} catch (error) {
|
|
callback(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = AzureStorageUploadStream;
|