Amazon s3中的节点文件上载错误

时间:2017-12-14 10:59:37

标签: javascript node.js amazon-s3 npm

在亚马逊s3上传文件时出错。 代码如下:

        const s3 = require('s3');  const client = s3.createClient({
             maxAsyncS3: 100,
             s3RetryCount: 3,
             s3RetryDelay: 30000,
             multipartUploadThreshold: 20971520,
             multipartUploadSize: 15728640,
             s3Options: {
                 accessKeyId: "xxxx",
                 secretAccessKey: "yyyy",
                 region: "us-east-2",
             },    });
            const params = {
             localDir: "file-path",
             s3Params: {
               Bucket: "bucket-name",
               Prefix: "images/image.jpg"
             },    };    
          const uploader = client.uploadDir(params); 
       uploader.on('error', (err) => {
             console.error("unable to upload:", err.stack);  
      });    
uploader.on('progress', () => {
             console.log("progress", uploader.progressMd5Amount,
                       uploader.progressAmount, uploader.progressTotal);    });    
uploader.on('end', () => {
             console.log("done uploading");    });

我得到的错误是:

  

无法上传:错误:不支持非文件流对象   在AWS.S3中使用SigV4

2 个答案:

答案 0 :(得分:1)

您应该将上传方法传递给文件。 这是一个例子:

  var file = files[0];
  var fileName = file.name;
  var albumPhotosKey = encodeURIComponent(albumName) + '//';

  var photoKey = albumPhotosKey + fileName;
  s3.upload({
    Key: photoKey,
    Body: file,
    ACL: 'public-read'
  }, function(err, data) {
    if (err) {
      return alert('There was an error uploading your photo: ', err.message);
    }
    alert('Successfully uploaded photo.');
    viewAlbum(albumName);
  });
}

更多示例:http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album-full.html

答案 1 :(得分:1)

            var params = {
                ACL: 'public-read', // assign this rule for the readable permissions
                Bucket: 'bucket-name-on-amazon-s3', // bucket name created on amazon s3
                Key: Date.now() + "_" + path.basename(fullUrl) // assign filename
            };

            var body = fs.createReadStream(fullUrl);
            var s3 = new AWS.S3({
                params: params
            });

            s3.upload({
                Body: body
            }).
            on('httpUploadProgress', function (evt) {
                var progress = Math.round(evt.loaded / evt.total) * 100;
                console.log('The progress ia '+progress);

            }).
            send(function (err, data) {
                console.log(err, data);

                //handle error
                if (err) {
                    console.log("Error", err);
                    req.flash('error_msg', 'failed to upload the file meta-data');
                    res.redirect('/upload');
                }

                //success
                if (data) {
                    console.log("Uploaded in:", data.Location); //url of the file on amazon s3
                    //build ad object
                    var person = new Person();
                    person.country = country;
                    person.save(function (err) {
                        if (err) {
                            req.flash('error_msg', 'failed to upload the file meta-data');
                            res.redirect('/upload');
                        } else {
                           // Do something - give alert to user, e.g. u can use   flash messages as well
                        }
                    });
                }
            });