'How to download a Signed Google Storage URL on client machine
I have this code that creates a signed URL to download a specific file in my bucket:
exports.download = functions.https.onRequest((req, res) => {
const userID = req.query.userID;
const downloadID = req.query.downloadID;
const fileName = req.query.fileName;
admin
.storage()
.bucket()
.file(`userUploads/${userID}/${downloadID}/${fileName}`)
.getSignedUrl({
version: "v4",
action: "read",
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
})
.then((signedURLArray) => {
const url = signedURLArray[0];
// return res.download(url);
return res.redirect(url);
})
.catch((err) => {
return res.send(err);
});
});
I've tried:
res.setHeader(
`"content-disposition", "attachment; filename=${fileName}"`
);
with res.redirect but it just gives me a HTTP_TOKEN_ERROR . If I use res.redirect() without setting any extra headers, it will just view the file on the browser, not download it - I only want the user to be able to download the file.
Obviously, res.download does not work as the file in question is not on the server, it is served on some other server where my bucket files are located (most likely some storage.googleapis.com domain)
UPDATE:
As you can see by the response request:
It's sending the right data back to the browser
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

