node.js & S3 / Write to s3 with knox

戏子无情 提交于 2019-12-13 02:18:00

问题


I'm trying to write to S3 with knox, by the following code:

var knox = require('knox');

var client = knox.createClient({
    key: 'key'
    , secret: 'pass'
    , bucket: S3_BUCKET
});

fs.stat("/opt/files/" + url, function(err, stats) {
     if (stats != null && stats.size != 0){
           var req = client.put(url, {
      'Content-Length': stats.size
        });
        req.on('error' ,function (err){
            console.log(err);
        })
        var readstr = fs.createReadStream("/opt/files/" + url);
        readstr.pipe(req);
        readstr.on('error', function (err){
          console.log(err);
        })

It gives me the following error for big files (I check file with 900MB):

{ [Error: write ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'write' }

The certificates are O.K, I success to log in with the key & secret access key to Amazon and other npm-packages. In addirion, I success to upload small files with Knox.

I know that it connection error, but I don't understand why it happens and how can I solve it.


回答1:


You are probably hitting the "large file" limit that exists on Amazon's side.

According to their FAQ, for objects larger than 100MB, users should use "multipart upload".

For this you could use the "know-mpu" module and your example would become

var knox = require('knox');
var MultiPartUpload= require('knox-mpu');

var client = knox.createClient({
    key: 'key'
    , secret: 'pass'
    , bucket: S3_BUCKET
});

var upload = new MultiPartUpload(
        {
            client: client,
            objectName: url,
            file: '/opt/files' + url
        },
        function(err, body) {
           console.log(body);
        }
    );



回答2:


You aren't doing anything with response. Try to add listener for a 'response' event on the request like

req.on('response', function(res){
  // ...
});


来源:https://stackoverflow.com/questions/25265259/node-js-s3-write-to-s3-with-knox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!