vignesh d
vignesh d

Reputation: 305

Is it possible to write directly to a file hosted in gcp using nodejs

I would like to know if there is any way to directly write to a file hosted in GCP/Google cloud storage bucket. Ideally we would like to write to the file using nodejs but open to explore other technologies too.

We are getting a request in NodeJS router method and we want to write the request content to a file in GCP bucket.It looks to me that only way to write to a file in gcp bucket is by uploading a file but we would like to see if there is any way to write to the file without having to upload the file.

Is there any way we can do this? any pointers or links will be very helpful.

Upvotes: 3

Views: 2530

Answers (2)

Cloud Ace Wenyuan Jiang
Cloud Ace Wenyuan Jiang

Reputation: 2205

Since you are open to other tools, how about mount the bucket into local file system using gcsfuse, then you can just write to the mount point in your nodejs request and let gcsfuse to do the upload part.

Upvotes: 2

Dan Starns
Dan Starns

Reputation: 3825

May i point you in the direction of file.createWriteStream where you can create a writable stream to pipe to a file.

const fs = require('fs');
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const file = myBucket.file('my-file');

fs.createReadStream('/path')
  .pipe(file.createWriteStream())
  .on('error', function(err) {})
  .on('finish', function() {
    // The file upload is complete.
  });

Upvotes: 3

Related Questions