I have a stream for writing some text to a cloud storage bucket.
On finish I want a message to be returned to the parent function.
I tried returning the bufferstream but this gives me the whole bufferstream object. I just want the message to be returned.
e.g. if I have another function calling toBucket I want the message that the file has been uploaded being returned so I can show it in the browser.
How can I fix this?
const toBucket = (message, filename) => {
const storage = new Storage();
// Initiate the source
const bufferStream = new stream.PassThrough();
// Write your buffer
bufferStream.end(Buffer.from(message));
const myBucket = storage.bucket(process.env.BUCKET);
const file = myBucket.file(filename);
// Pipe the 'bufferStream' into a 'file.createWriteStream' method.
bufferStream
.pipe(
file.createWriteStream({
validation: 'md5',
})
)
.on('error', (err) => {
// eslint-disable-next-line no-console
console.error(err);
})
.on('finish', () => {
// The file upload is complete.
const message = `${filename} is uploaded!`;
// eslint-disable-next-line no-console
console.log(message);
return message;
});
};
for use in
() => async {
await things happening...
const saved = toBucket(message,filename);
sendToBrowser(saved);
}