I'm trying to write an Express route that takes an image URI in the POST body and then saves the image into a Google Cloud Storage Bucket.
I'm not able to persist this image to local disk and need to stream the buffer straight to the GCS bucket.
My route creates a 4KB "stub" in the GCS bucket but there's no image payload. My nodejs then proceeds to crash...
Q: What is the correct way to .pipe() the results of the https.request() to blob.createWriteStream()? Is this the right approach?
I've spent a few days trying to find the right solution with different stream handlers but with precious little progress to show for it. Can someone help?
message: 'The rate of change requests to the object my-projectID/testimage.jpg exceeds the rate limit. Please reduce the rate of create, update, and delete requests.'
const streamifier = require('streamifier');
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
projectId: 'my-projectID',
keyFile: '../config/my-projectID.json'
});
const bucket = storage.bucket('my-projectID');
const blob = bucket.file('testimg.jpg');
app.post('/bam', passport.authenticate('basic', {session: false }), (req, res) => {
return new Promise((resolve, reject) => {
https.request(req.body.pic, (response) => {
response.on('data', (d) => {
streamifier.createReadStream(d)
.pipe(blob.createWriteStream({
resumable:false,
public:true,
metadata:{ contentType: 'image/jpeg' }
}));
});
response.on('finish', (done) => {
console.log(done);
});
response.on('error', (err) => {
console.log(err);
});
}).end();
});
});
** Apologies for my ugly JS, I'm still at the bottom of ES6 learning curve.