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

451
Vistas
GCS catch error if file not found in bucket while streaming download

I am trying to fetch images from google cloud storage bucket from browser and serving the files using API in express. Following code breaks when the Image path is invalid. The try catch doesn't catch the file not found error. Am I missing something here? This code works if an image file exists.

From the Express side, I am using wildcard route (i.e. /*) as the API can accept any image path coming in and try to serve it.

const express = require('express')
const {Storage} = require('@google-cloud/storage');
let server = express()
const storage = new Storage();
const bucketName = '<some-bucket-name>'

server.get('/*', async (req, res) => {
  const widthString = req.query.width
  const heightString = req.query.height
  const format = req.query.format
  const fileName = req.path.substring(1);
  console.log("url: ", req.path)
  let width, height
  if (widthString) {
    width = parseInt(widthString)
  }
  if (heightString) {
    height = parseInt(heightString)
  }
  res.type(`image/${format || 'png'}`)
  
  try{
      await storage.bucket(bucketName).file(fileName).createReadStream().pipe(res)
  } catch (err) {
     console.error(err);
  }
})

Error:

url:  /media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
events.js:377
      throw er; // Unhandled 'error' event
      ^

ApiError: No such object: assets/media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
    at new ApiError (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:73:15)
    at Util.parseHttpRespMessage (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:175:41)
    at Util.handleResp (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:149:76)
    at Duplexify.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/storage/build/src/file.js:888:31)
    at Duplexify.emit (events.js:400:28)
    at PassThrough.emit (events.js:400:28)
    at onResponse (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:222:19)
    at PassThrough.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:163:11)
    at PassThrough.emit (events.js:412:35)
    at /home/ubuntu/imageoptimizer/node_modules/teeny-request/build/src/index.js:191:27
Emitted 'error' event on PassThrough instance at:
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 404,
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Posting my previous comment as an answer for visibility

You have to handle this exception by yourself. GCP won't throw the error directly. It only returns 404 as an output, and you have to handle it manually rather than expecting try{} catch () {} to catch this exception. Or you can request it as a new feature in issue tracker, however I am not sure how long it will take for the Google to implement this feature.

about 4 years ago · Juan Pablo Isaza 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