Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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:

enter image description here

It's sending the right data back to the browser

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As you redirect to google's server, it becomes responsible for the headers of the response, and specifically for the Content-disposition one. Metadata of the files stored in google storage can be set up on or after the file is uploaded. Your header should look like:

Content-Disposition: attachment; filename="file.ext..."

Please check these docs to see possible options on how to set the metadata for the files manually via gsutil:

  • https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata
  • https://cloud.google.com/storage/docs/metadata#content-disposition

UPDATE

In order to generate signed URL with custom header (e.g. Content-Disposition) with the Google storage admin API it should be added via a config parameter responseDisposition with desired value

Usage example:

...
admin
    .storage()
    .bucket()
    .file(`userUploads/${userID}/${downloadID}/${fileName}`)
    .getSignedUrl({
      responseDisposition: "attachment",
      version: "v4",
      action: "read",
      expires: Date.now() + 15 * 60 * 1000, // 15 minutes
    })
...

See MDN reference for possible header values here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

See documentation for nodejs api library for google storage here and link to the lines where file attachment properties, promptSaveAs and responseDisposition are explained here.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda