The solution, I believe, is to use the file.createWriteStream function, which the bucket.upload function completes in the Google Cloud Node SDK.
I have very little experience with threads, so try to carry me if this does not work.
First of all, we need to take the base64 data and transfer it to the stream. To do this, we are going to include the stream library, create a buffer from base64 data and add a buffer to the end of the stream.
var stream = require('stream'); var bufferStream = new stream.PassThrough(); bufferStream.end(new Buffer(req.body.base64Image, 'base64'));
Learn more about base64 decoding and stream creation .
We are going to transfer the stream to the recording stream created by the file.createWriteStream function.
var gcs = require('@google-cloud/storage')({ projectId: 'grape-spaceship-123', keyFilename: '/path/to/keyfile.json' }); //Define bucket. var myBucket = gcs.bucket('my-bucket'); //Define file & file name. var file = myBucket.file('my-file.jpg'); //Pipe the 'bufferStream' into a 'file.createWriteStream' method. bufferStream.pipe(file.createWriteStream({ metadata: { contentType: 'image/jpeg', metadata: { custom: 'metadata' } }, public: true, validation: "md5" })) .on('error', function(err) {}) .on('finish', function() { // The file upload is complete. });
Information about file.createWriteStream , File documents , bucket.upload and bucket.upload method code in Node SDK .
Thus, how the above code works, you need to determine the bucket in which you want to put the file, and then determine the file and file name. We do not set download options here. Then we pass the bufferStream variable that we just created into the file.createWriteStream method that we discussed earlier. In these options, we define the metadata and other parameters that you want to implement. It was very helpful to take a look at the Node code on Github to find out how they break the bucket.upload function and recommend that you do this as well. Finally, we attach a couple of events when the download ends and when it crashes.