Save incoming file from s3 w / nodejs & knox?

This is most likely very simple because the documents are not counted ... from knox documents:

"The following is an example of a GET request in a file that we just clicked on s3 and simply displays the response status code, headers and body."

client.get('/test/Readme.md').on('response', function(res){
  console.log(res.statusCode);
  console.log(res.headers);
  res.setEncoding('utf8');
  res.on('data', function(chunk){
    console.log(chunk);
  });
}).end();

Easy enough, but how to save incoming data as a local file? new BufferList () or something else?

I am trying to create a service for resizing images on the fly, which downloads images from s3 or cloud mode and returns their size based on the request. Then the browser caches dimensional images instead of full ones directly from s3. Of course, first I need this base bit! Any ideas?

Thanks guys!

+3
source share
2

, knox API, stream.pipe() . , , , S3, , , .

"" var outstream = fs.createWriteStream(filename);. "data" outstream.write(chunk); , "end", .

+2

:

var buffer = '';
client.get('/test/Readme.md').on('response', function(res){

  res.setEncoding('utf8');

  res.on('data', function(chunk){
    buffer += chunk;
  });

  res.on('end', function(){
    // do something with the buffer such as save it to file, 
    // or directly resize the image here.
    // eg. save to file:
    fs.writeFile('downloaded_readme.md', buffer, 'utf8', function (err) {
    });
  });

}).end();
+2

Source: https://habr.com/ru/post/1795834/


All Articles