39

I'm running the following code and it fails with the below error.

AWS Code to list all objects inside a bucket.

const http = require('http');
const host = '127.0.0.1';
const port = 5000;
const path = require('path');
const url = require('url');
const fs = require('fs');
var AWS = require('aws-sdk');



const laptopDate = JSON.parse(fs.readFileSync(`${__dirname}/data/data.json`, `utf-8`));

AWS.config.update({accessKeyId: '***', secretAccessKey: '***', region: 'ap-south-1'});
s3 = new AWS.S3({apiVersion: '2006-03-01'});

var params = { 
    Bucket: 'bucket-name'
};

const server = http.createServer(function(req, res){
    const path = url.parse(req.url, true).pathname;
    const id = url.parse(req.url, true).query.id;

    if (path === 'bucket' || path === '/')
      s3.listObjects(params, function (err, data) {
        if(err) throw err;
        res.writeHead(200, { 'Content-Type': 'text/html' });
        //const output = JSON.parse(data)
        console.log(data);
        res.end(data);
      });
});

server.listen(port, host, function(req, res) {
    console.log(`Server is listening on ${host} and ${port}`)
});

The first output which is console.log displays everything as expected. However the res.end to render the output to the screen fails with the below error.

The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object
    at ServerResponse.end (_http_outgoing.js:752:13)
    at Response.<anonymous> (D:\js\Extra\starter\index.js:30:13)
    at Request.<anonymous> (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:364:18)
    at Request.callListeners (D:\js\Extra\starter\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (D:\js\Extra\starter\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (D:\js\Extra\starter\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at D:\js\Extra\starter\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:38:9) {
  message: 'The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object',
  code: 'ERR_INVALID_ARG_TYPE',
  time: 2020-05-18T08:39:24.916Z
}
varad_s
  • 764
  • 1
  • 12
  • 24
anish anil
  • 2,299
  • 7
  • 21
  • 41
  • If the response from S3 is an object, then just try -> `res.json(data)` – Keith May 18 '20 at 08:55
  • I think it is probably good idea to remove AWS config params from this question :) – Lazar Nikolic May 18 '20 at 08:55
  • it fails even if i use res.json ************* D:\js\Extra\starter\node_modules\aws-sdk\lib\request.js:31 throw err; ^ Error [TypeError]: res.json is not a function at Response. (D:\js\Extra\starter\index.js:30:13) at Request. (D:\js\Extra\starter\node_modules\aws-sdk\lib – anish anil May 18 '20 at 09:59
  • @LazarNikolic, That was not the actual value of the AWS Keys ;-). Else by now i would have had multiple M# Large VM's running on my AWS Account:-) – anish anil May 18 '20 at 10:00

4 Answers4

38

Remove this res.writeHead(200, { 'Content-Type': 'text/html' });

And instead of res.end(data) use res.send(data) or better yet res.send({ data }).


EDIT I didn't notice that you didn't use express, try this:

res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
zishone
  • 1,196
  • 1
  • 9
  • 11
  • Still fails, message: 'res.send is not a function', code: 'TypeError', time: 2020-05-18T12:59:37.706Z } Note I'm not using Express – anish anil May 18 '20 at 13:02
  • It still fails --> The first argument must be of type string or an instance of Buffer. Received an instance of Object – anish anil May 19 '20 at 08:36
  • 2
    Sorry, do this `res.write(JSON.stringify(data));` – zishone May 19 '20 at 08:47
  • 1
    That works Perfectly. Could you please explain why we used JSON.stringify and when should i use it and other methods available?? – anish anil May 19 '20 at 12:19
  • It's because `data`'s data type is an `object/json`, and `res.write()` accepts data type `string` or `buffer`. `JSON.stringify` converts the `object` to a `string` basically. – zishone May 19 '20 at 12:46
12

In case this may help someone, try to use the objectMode property.

something like it

const t = new Transform({
    objectMode: true, // set this one to true
    transform(data, _, done) {
      //...your code
    }
  });
Raphael Pinel
  • 2,352
  • 24
  • 26
2

In case this may help someone, I was using data type other than String, Number to be exact, so I changed it to String to resolve this error

Kritish Bhattarai
  • 1,501
  • 15
  • 20
0

In case of ExpressJS, the following solution worked for me:

const response = await s3client.send(command);
// The Body object also has 'transformToByteArray' and 'transformToWebStream' methods.
const data = await response.Body.transformToByteArray()
const buffer = Buffer.from(data);       
res.attachment(`filename.pdf`).contentType("application/pdf").send(buffer)