How to upload an in memory file data to google cloud storage using nodejs?

前端 未结 3 893
鱼传尺愫
鱼传尺愫 2020-12-14 10:57

I am reading an image from a url and processing it. I need to upload this data to a file in cloud storage, currently i am writing the data to a file and uploading this file

3条回答
  •  渐次进展
    2020-12-14 11:18

    The data can be uploaded without writing to a file by using nodes streams.

    const stream     = require('stream'),
          dataStream = new stream.PassThrough(),
          gcFile     = cloudStorage.bucket(bucketName).file(fileName)
    
    dataStream.push('content-to-upload')
    dataStream.push(null)
    
    await new Promise((resolve, reject) => {
      dataStream.pipe(gcFile.createWriteStream({
        resumable  : false,
        validation : false,
        metadata   : {'Cache-Control': 'public, max-age=31536000'}
      }))
      .on('error', (error : Error) => { 
        reject(error) 
      })
      .on('finish', () => { 
        resolve(true)
      })
    })
    

提交回复
热议问题