I'm developing a corporative CRUD application, and one of the features is storing the subscribed user profile picture to Google Cloud, and downloading it when it is needed to display it.
The fact is, when I need to download it, the request comes from the client front end, calling and specific GET route - for example, '/getPic'.
Then my Node back-end server handles this request, communicating with Google Cloud and downloading the file, more or less (simplified) as follows:
let localFile = fs.createWriteStream('temp/writeStreamFile.jpeg');
return new Promise((resolve,reject) =>{
storage.bucket().file(`users/user123456/profilePicture.jpeg`)
.createReadStream()
.on('end', () => {
console.log("ended");
resolve();
})
.on('response', ans => {
console.log("responded");
})
.on('error', err => {
console.error("Error", err);
reject();
})
.pipe(localFile);
})
Afterwards, I return this picture in the response of the HTTP request, and it's fine. The drawback of this operation is that the downloaded file remains stored in my webserver, in the folder ('temp/writeStreamFile.jpeg'), consuming my server storage.
Is there any way to "pipe" the file directly in the response of the HTTP request without saving it locally?