20

次のコードを実行していますが、以下のエラーで失敗します。

バッカー内のすべてのオブジェクトを一覧表示する AWS コード

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}`)

console.log である最初の出力には、期待どおりにすべてが表示されます。ただし、出力を画面にレンダリングする res.end は、以下のエラーで失敗します。

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
}
4

2 に答える 2