'How to pipe the res file to google cloud storage stream to save file on gs?
import {Storage} from "@google-cloud/storage";
this.post(
"/webhook",
async (req, res) => {
const storage = new Storage({/** google credential filename */ });
const clientBucket = storage.bucket(/** bucket name */);
// Create a reference to a file object
const fileName = `${moment().format("YYYYMMDDHHmmss")}.tar.gz`;
const file = clientBucket.file(fileName);
req.pipe(file.createWriteStream());
req.on("end", () => {
// have to update in db that file has been uploaded
});
})
Running this code on my local node server which is not uploading the file on gs storage. This is the post-call in this. I am receiving the stream data in req. I am not sure what I am doing wrong. If I write the req.pipe() in a local folder, it creates a file. Please note I am not getting any errors in the terminal.
Solution 1:[1]
You need to write JSON file to /tmp directory using fs.createWriteStream and then pipe file from /tmp to Storage using Storage API.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladyslav Yukhanov |

