Estoy tratando de obtener imágenes del cubo de google cloud storage
desde el navegador y sirviendo los archivos usando API en Express. El siguiente código se rompe cuando la ruta de la imagen no es válida. Try catch no detecta el error de archivo no encontrado. ¿Me estoy perdiendo de algo? Este código funciona si existe un archivo de imagen.
Desde el lado Express, estoy usando la ruta comodín (es decir, /*) ya que la API puede aceptar cualquier ruta de imagen que ingrese e intentar servirla.
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,
Publicar mi comentario anterior como una respuesta para la visibilidad.
Tienes que manejar esta excepción por ti mismo. GCP no arrojará el error directamente. Solo devuelve 404 como salida, y debe manejarlo manualmente en lugar de esperar que try{} catch () {}
detecte esta excepción. O puede solicitarlo como una nueva función en el rastreador de problemas , sin embargo, no estoy seguro de cuánto tiempo le llevará a Google implementar esta función.