简体   繁体   English

使用Node / Knox上传到S3:套接字挂起

[英]Uploading to S3 with Node/Knox : socket hang up

I'm trying to get() files and stream them to my s3 bucket, could someone tell me what I'm doing wrong? 我正在尝试获取()文件并将其流式传输到s3存储桶,有人可以告诉我我做错了什么吗?

My code : 我的代码:

var client = knox.createClient({
    key: 'AAAAAAAAAAAA',
    secret: 'BBBBBBBBBBBB',
    bucket: 'my-imgs',
});
var elem = list.shift(); // {_id:'filename.jpg', main_img: 'http://example.com/file.jpg'}
  request.get(elem.main_img,function(err,res){
      var headers = {
          'Content-Length': res.headers['content-length'],
          'Content-Type': res.headers['content-type'],
          'x-amz-acl': 'public-read'
      };
      console.log(headers) //outputs are ok, error comes after
      var req = client.putStream(res, elem._id, headers,function(err,s3res){
        if(err) console.log(err);
        console.log(s3res);
      });
  }).on('error', function(err) {
    console.log(err)
  });

The headers object is filled properly, the request doesn't error anything, and after a few seconds I get : 标头对象已正确填充,请求没有任何错误,几秒钟后我得到:

{ [Error: socket hang up] code: 'ECONNRESET' }

Do I have to configure my bucket in a particular way for it to accept transfers? 我是否必须以特殊的方式配置我的存储桶以使其接受传输? I've just created it through the AWS console, in 'US standard' and have done only one thing, adding a poicy that I tought would allow upload. 我刚刚通过AWS控制台以“美国标准”创建了它,并且只做了一件事情,增加了我可以允许上传的一点趣味。 I have the feeling that it may be the problem, but I have no idea as of how to fix it, and all the tutorials I see are very outdated, please give me a clue! 我感觉这可能是问题所在,但是我不知道如何解决它,而且我看到的所有教程都已经过时了,请给我一个提示!

The policy : 政策 :

{
    "Statement": [
        {
            "Sid": "allow-public-read",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-imgs/*"
        },
        {
            "Sid": "allow-public-put",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::my-imgs/*"
        }
    ]
}

You can try this in ur app.js 您可以在ur app.js中尝试

var http = require('http');

//increase the max socket limit
http.globalAgent.maxSockets = 1024;

Last week I was struggling with this same issue. 上周,我在同一个问题上苦苦挣扎。 Gladly I found the solution: in order of it to work, you need to end your S3 request: 很高兴我找到了解决方案:为了使其正常工作,您需要结束您的S3请求:

var req = client.putStream(res, elem._id, headers,function(err,s3res){
    if(err) console.log(err);
    console.log(s3res);
});
req.end();

Or Simply: 或简单地说:

var req = client.putStream(res, elem._id, headers,function(err,s3res){
    if(err) console.log(err);
    console.log(s3res);
}).end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM