1

I wanted to display a file from s3 bucket when the URL "/file/key" is called

app.get("/file/:key", getFileFromS3);

exports.getFileFromS3 = async (req, res, next) => {
  const s3 = new awsS3();
  const { key } = req.params;

  const data = await s3.getFileObject(key); //calling some other function
  console.log("data",data) 
  res.write(data.Body, "binary");
  res.end(null, "binary");
};

And the data looged in the console is

{
  AcceptRanges: 'bytes',
  LastModified: 2021-01-02T10:16:16.000Z,
  ContentLength: 140070,
  ETag: '"f1f9e2921a5fd0e3d748088f2df041f2"',
  ContentType: 'application/octet-stream',
  Metadata: {},
  Body: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 07 d0 00 00 03 e8 08 06 00 00 00 86 5e 65 aa 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 20 00 ... 140020 more bytes>
}

so when I hit the URL https://npk-api.herokuapp.com/file/1609575283092_driver.png in my browser I'm able to see the image.

But when I'm giving that URL to a third party (SIGNZY) it says

verify Failed Only image(s)/pdf(s) are allowed, one of the urls does not respond like an image

But when i upload the same image in an online website and get the link i.e https://i.ibb.co/QYn0j82/driver.png , SIGNZY doesn't throw any error..

So its clear that I'm not sending the response correctly when we hit https://npk-api.herokuapp.com/file/1609575283092_driver.png

Can Someone help me in figuring out this issue..

Note: I need a genric solution which works for both images and pdfs..

Update:

s3.getObject(params)
    .on('httpHeaders', function (statusCode, headers) {
        res.set('Content-Length', headers['content-length']);
        res.set('Content-Type', headers['content-type']);
        this.response.httpResponse.createUnbufferedStream()
            .pipe(res);
    })
    .send();
vamsi
  • 231
  • 2
  • 8
  • You could try to stream the response, see this for example: https://stackoverflow.com/questions/35782434/streaming-file-from-s3-with-express-including-information-on-length-and-filetype – eol Jan 02 '21 at 11:28
  • @eol using that updated code file is getting downloaded, But i need to show the file on hitting that URL.. – vamsi Jan 02 '21 at 12:33

0 Answers0