使用 AWS SDK 上传 Node JS 文件 | 错误:无法确定 [object Object] 的长度

ste*_*eph 5 amazon-s3 node.js sails.js aws-sdk

我正在尝试在我的 sails js 应用程序上使用 AWS SDK。我不断收到此错误:无法确定 [object Object] 的长度。有谁知道这是什么原因造成的?

这是代码:

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");
                }
              });
Run Code Online (Sandbox Code Playgroud)

Jai*_*nay 4

根据AWS

theBody的值应为buffer、blob 或 Stream

upload(params = {}, [options], [callback])
Run Code Online (Sandbox Code Playgroud)

如果有效负载足够大,则使用智能并发处理部分来上传任意大小的缓冲区、blob 或流。您可以通过设置选项来配置并发队列大小。

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

您需要像流式传输文件fs.createReadStream(req.file.path)并将其发送到Body参数中