Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

138
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,
7 months 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.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos