Uploading a buffer to google cloud storage

后端 未结 4 606
无人及你
无人及你 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条回答
  • 2021-02-07 06:44

    .save to save the day! Some code below where I save my "pdf" that I created.

    https://googleapis.dev/nodejs/storage/latest/File.html#save

    const { Storage } = require("@google-cloud/storage");
    
    const gc = new Storage({
      keyFilename: path.join(__dirname, "./path to your service account .json"),
      projectId: "your project id",
    });
    
          const file = gc.bucket(bucketName).file("tester.pdf");
          file.save(pdf, (err) => {
            if (!err) {
              console.log("cool");
            } else {
              console.log("error " + err);
            }
          });
    
    0 讨论(0)
  • 2021-02-07 06:59

    We have an issue about supporting this more easily: https://github.com/GoogleCloudPlatform/gcloud-node/issues/1179

    But for now, you can try:

    file.createWriteStream()
      .on('error', function(err) {})
      .on('finish', function() {})
      .end(fileContents);
    
    0 讨论(0)
  • 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();
      });
    
    0 讨论(0)
  • 2021-02-07 07:06

    The following snippet is from a google example. The example assumes you have used multer, or something similar, and can access the file at req.file. You can stream the file to cloud storage using middleware that resembles the following:

    function sendUploadToGCS (req, res, next) {
      if (!req.file) {
        return next();
      }
    
      const gcsname = Date.now() + req.file.originalname;
      const file = bucket.file(gcsname);
    
      const stream = file.createWriteStream({
        metadata: {
          contentType: req.file.mimetype
        },
        resumable: false
      });
    
      stream.on('error', (err) => {
        req.file.cloudStorageError = err;
        next(err);
      });
    
      stream.on('finish', () => {
        req.file.cloudStorageObject = gcsname;
        file.makePublic().then(() => {
          req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
          next();
        });
      });
    
      stream.end(req.file.buffer);
    }
    
    0 讨论(0)
提交回复
热议问题