Uploading a buffer to google cloud storage

后端 未结 4 611
无人及你
无人及你 2021-02-07 06:27

I\'m trying to save a Buffer (of a file uploaded from a form) to Google Cloud storage, but it seems like the Google Node SDK only allows files with a given path to be uploaded (

4条回答
  •  梦毁少年i
    2021-02-07 07:02

    This is actually easy:

      let remotePath = 'some/key/to/store.json';
      let localReadStream = new stream.PassThrough();
      localReadStream.end(JSON.stringify(someObject, null, '   '));
    
      let remoteWriteStream = bucket.file(remotePath).createWriteStream({ 
         metadata : { 
            contentType : 'application/json' 
         }
      });
    
      localReadStream.pipe(remoteWriteStream)
      .on('error', err => {
         return callback(err);      
      })
      .on('finish', () => {
         return callback();
      });
    

提交回复
热议问题