Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS file upload with AWS SDK | Error: Cannot determine length of [object Object]

I'm trying to use the AWS SDK on my sails js app. I keep getting this error: Cannot determine length of [object Object]. Does anyone know whats causing this?

Here is the code:

var AWS = require('aws-sdk');

              var s3bucket = new AWS.S3({
                accessKeyId : 'xxx',
                secretAccessKey : 'xxx',
                params: {Bucket: 'sweetestspots'}
              });
              var body = req.file('cover');

              s3bucket.upload({Key: 'filename', ACL:'public-read', Body:body}, function(err, data) {
                console.log ("data")
                console.log (data)
                if (err) {
                  console.log("Error uploading data: ", err);
                  return res.send("err");

                } else {
                  console.log("Successfully uploaded data to myBucket/myKey");
                  console.log(data);

                  return res.send("uploaded");
                }
              });
like image 997
steph Avatar asked Dec 24 '22 03:12

steph


1 Answers

according to the aws

the Body should have a value of buffer, blob, or stream

upload(params = {}, [options], [callback])

Uploads an arbitrarily sized buffer, blob, or stream, using intelligent concurrent handling of parts if the payload is large enough. You can configure the concurrent queue size by setting options.

var params = {Bucket: 'bucket', Key: 'key', Body: stream};
s3.upload(params, function(err, data) {
    console.log(err, data);
});

you need to stream your file like fs.createReadStream(req.file.path) and send put it in the Body parameter

like image 85
Jairo Malanay Avatar answered May 21 '23 10:05

Jairo Malanay