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

118
Vistas
Resizing images with sharp before uploading to google cloud storage

I tried to resize or compress an image before uploading to the google cloud storage. The upload works fine but the resizing does not seem to work.

Here is my code:

const uploadImage = async (file) => new Promise((resolve, reject) => {
    let { originalname, buffer } = file
    sharp(buffer)
        .resize(1800, 948)
        .toFormat("jpeg")
        .jpeg({ quality: 80 })
        .toBuffer()


    const blob = bucket.file(originalname.replace(/ /g, "_"))
    const blobStream = blob.createWriteStream({
        resumable: false
    })
    blobStream.on('finish', () => {
        const publicUrl = format(
            `https://storage.googleapis.com/${bucket.name}/${blob.name}`
        )
        resolve(publicUrl)
    }).on('error', () => {
            reject(`Unable to upload image, something went wrong`)
        })
        .end(buffer)
}) 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I ran into the same issue with a project I was working on. After lots of trial and error I found the following solution. It might not be the most elegant, but it worked for me.

In my upload route function I created a new thumbnail image object with the original file values and passed it as the file parameter to the uploadFile function for google cloud storage.

Inside my upload image route function:

const file = req.file;

const thumbnail = {
  fieldname: file.fieldname,
  originalname: `thumbnail_${file.originalname}`,
  encoding: file.encoding,
  mimetype: file.mimetype,
  buffer: await sharp(file.buffer).resize({ width: 150 }).toBuffer()
}

const uploadThumbnail = await uploadFile(thumbnail);

My google cloud storage upload file function:

const uploadFile = async (file) => new Promise((resolve, reject) => {

  const gcsname = file.originalname;
  const bucketFile = bucket.file(gcsname);

  const stream = bucketFile.createWriteStream({
    resumable: false,
    metadata: {
      contentType: file.mimetype
    }
  });

  stream.on('error', (err) => {
    reject(err);
  });

  stream.on('finish', (res) => {
    resolve({ 
      name: gcsname
    });
  });

  stream.end(file.buffer);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think the problem is with toFormat(). That function does not exist in the Docs. Can you try to remove it and check if it would work?

sharp(buffer)
  .resize(1800, 948)
  .jpeg({ quality: 80 })
  .toBuffer()
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