title: Error:Input buffer contains unsupported image format while using request library to download signed aws s3 image and passing to sharp
body: <p>I have a node js project which requires to reduce the size of images before sending to the client. I am using <a href="https://www.npmjs.com/package/request" rel="nofollow noreferrer">request</a> library and <a href="https://sharp.pixelplumbing.com/en/stable/" rel="nofollow noreferrer">sharp</a> library.</p>
  
  <p>The source of the images is a signed aws s3 bucket url - e.g </p>
  
  <pre><code>{http://my_intentionally_hidden_url/api/getImage?image=https://my_intentionally_hidden_bucket_name_and_path.s3.eu-central-1.amazonaws.com/name_it/q_i_9.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=My_intentionally_hidden_credentials%2F20190813%2Feu-central-1%2Fs3%2Faws4_request&amp;X-Amz-Date=20190813T221026Z&amp;X-Amz-Expires=120&amp;X-Amz-Signature=1c81c2b69d82e116d8ea398604b44dfb3ca2bb7d9b961d639766630fe4b4eb95&amp;X-Amz-SignedHeaders=host&amp;width=1440&amp;height=120}
  </code></pre>
  
  <p>I am using the following block of code to get the aboove url from s3, resize it and then send the resized image back to the client</p>
  
  <pre><code>module.exports.getImage = (req,res) =&gt; {
    const url = req.query.image; //the signed url
    var width = parseInt(req.query.width);
    var height = parseInt(req.query.height);
  
    if(width &gt; 300){
        width = 300;
    }
  
    if(height &gt; 300){
        height = 300;
    }
  
    request({url, encoding: null }, (error, response, body ) =&gt; {
        console.log({body});
        console.log({error});
  
  
        if(!error) {
           sharp(body)
              .resize (width,height)
              .webp({ lossless: false })
              .toBuffer()
              .then(data =&gt; {
                  console.log("we we we")
                  return res.send(data);
                  console.log({data});
              })
              .catch(err =&gt; {
                  console.log({err})
              })
  
        }
        else{
            console.log({error})
        }
     })
  }
  </code></pre>
  
  <p>In the above call, </p>
  
  <p>The url was signed a couple of minutes ago to make it public, and the result of the console.log({body}) is binary data, which is what should be returned,</p>
  
  <p>The first console.log({error}) is null. </p>
  
  <p>The problem is where the console.log({err}) is and it shows - </p>
  
  <pre><code>{ err: Error: Input buffer contains unsupported image format }
  </code></pre>
  
  <p>I have another bucket on s3 and if i take the url of a public image (url that is not signed) on that bucket and do - </p>
  
  <pre><code> ....
   const url = "https://My_hidden_bucket_name.s3-eu-west-1.amazonaws.com/o_d_3.jpg"
   var width = parseInt(req.query.width);
   var height = parseInt(req.query.height);
   ....
  </code></pre>
  
  <p>I am sure that the signed urls's are working because the signed url's have been tested when they are  called from the client and the image shows.</p>
  
  <p>I have tried using sharp without the .webp() and testing it out and i get the same result, i have used it with the other formats that sharp offers - jpeg, png and the same error message comes up. It appears request is returning the right response.</p>