how to load an image from url into buffer in nodejs

前端 未结 5 935
谎友^
谎友^ 2020-12-29 19:31

I am new to nodejs and am trying to set up a server where i get the exif information from an image. My images are on S3 so I want to be able to just pass in the s3 url as a

5条回答
  •  一整个雨季
    2020-12-29 20:34

    I was able to solve this only after reading that encoding: null is required and providing it as an parameter to request.

    This will download the image from url and produce a buffer with the image data.

    Using the request library -

    const request = require('request');
    
    let url = 'http://website.com/image.png';
    request({ url, encoding: null }, (err, resp, buffer) => {
         // Use the buffer
         // buffer contains the image data
         // typeof buffer === 'object'
    });
    

    Note: omitting the encoding: null will result in an unusable string and not in a buffer. Buffer.from won't work correctly too.

    This was tested with Node 8

提交回复
热议问题