Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!