Skip to content

Instantly share code, notes, and snippets.

@Saccarab
Created February 24, 2019 14:12
Show Gist options
  • Save Saccarab/8d6fdcb6d8899dd32dbd0472dc4af938 to your computer and use it in GitHub Desktop.
Save Saccarab/8d6fdcb6d8899dd32dbd0472dc4af938 to your computer and use it in GitHub Desktop.
stream file to client from gcs through express server
import request from 'request'
import fs from 'fs'
const requestQuery = {
path: path
}
request({url: 'http://localhost:8080/getFile', qs: requestQuery})
.on('response', (response) => {
//response chunks
})
.pipe(fs.createWriteStream(`${WRITEPATH}`)
.on('error', (err) => {
// handle error
})
.on('finish', () => {
// handle success
})
)
app.get('/getFile', (req, res) => {
const {path} = req.query
const remoteFile = bucket.file(`${FILEPATH}`)
remoteFile.createReadStream()
.on('error', (error) => {
res.status(500)
res.end()
})
.on('end', () => {
res.end()
})
.pipe(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment