Categories
aws-sdk-nodejs javascript node.js nodejs-server nodejs-stream

The “chunk” argument must be of type string or an instance of Buffer

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

AWS Code to list all objects inside a bucker

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
}

Any help is greatly appreciated.

Thank you
Anish