Created
February 24, 2019 14:12
-
-
Save Saccarab/8d6fdcb6d8899dd32dbd0472dc4af938 to your computer and use it in GitHub Desktop.
stream file to client from gcs through express server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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