首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ERR_CONNECTION_RESET时PUTting到S3

ERR_CONNECTION_RESET时PUTting到S3
EN

Stack Overflow用户
提问于 2014-04-01 23:22:42
回答 3查看 7.8K关注 0票数 14

我是通过ajax请求到PUTting的S3文件,大约有50%的时间我会得到ERR_CONNECTION_RESET错误。

我知道这些请求是正确的同样,这是一个间歇性的问题,我从多个位置和机器上看到。

下面是我用来将文件放入S3的相关coffeescript代码。它来自米迦·罗伯逊和罗克·克鲁莱克在http://micahroberson.com/upload-files-directly-to-s3-w-backbone-on-heroku/http://codeartists.com/post/36892733572/how-to-directly-upload-files-to-amazon-s3-from-your的工作。

代码语言:javascript
运行
复制
  createCORSRequest: (method, url) ->
    xhr = new XMLHttpRequest()

    if xhr.withCredentials?
      xhr.open method, url, true
    else if typeof XDomainRequest != "undefined"
      xhr = new XDomainRequest()
      xhr.open method, url
    else
      xhr = null

    xhr

  uploadToS3: (file, signature) ->
    this_s3upload = this
    this_s3upload.signature = signature
    url = signature.signed_request

    xhr = @createCORSRequest 'PUT', decodeURIComponent(signature.signed_request)

    if !xhr
      @onError 'CORS not supported'
    else
      xhr.onload = () ->
        if xhr.status == 200
          this_s3upload.onProgress 100, 'Upload completed.'
          this_s3upload.onFinishS3Put file, this_s3upload.signature
        else
          this_s3upload.onError file, 'Upload error: ' + xhr.status

      xhr.onerror = () ->
        this_s3upload.onError file, 'XHR error.', this_s3upload.signature

      xhr.upload.onprogress = (e) ->
        if e.lengthComputable
          percentLoaded = Math.round (e.loaded / e.total) * 100

          if percentLoaded == 100
            message = "Finalizing"
          else
            message = "Uploading"

          this_s3upload.onProgress xhr, file, percentLoaded, message, this_s3upload.signature

      xhr.onabort = ->
        this_s3upload.onAbort file, "XHR cancelled by user.", this_s3upload.signature

    xhr.setRequestHeader 'Content-Type', file.type
    xhr.setRequestHeader 'x-amz-acl', 'public-read'
    xhr.send file

更新

在这个问题上,我得到了亚马逊的大力支持。根据他们的建议,我创建了一个EC2窗口实例,在上面加载了Chrome,并尝试用我的代码上传了5个文件10次。我一次也没看到这个错误。我确实偶尔看到一些SignatureDoesNotMatch错误,但没有看到一个ERR_CONNECTION_RESET错误。虽然在我使用的每个非EC2客户机/网络位置上,我仍然看到ERR_CONNECTION_RESET错误。

更新在这里仍然没有解决方案。我已经从使用自滚签名算法转移到由boto提供的签名算法。不过,对ERR_CONNECTION_RESET问题没有任何影响。

EN

回答 3

Stack Overflow用户

发布于 2016-12-06 16:29:31

在使用预先签名的URL上载更大的文件(长请求)时,我遇到了这个问题,下面是Heroku的例子 (node ):

代码语言:javascript
运行
复制
app.get('/sign-s3', (req, res) => {
  const s3 = new aws.S3();
  const fileName = req.query['file-name'];
  const fileType = req.query['file-type'];
  const s3Params = {
    Bucket: S3_BUCKET,
    Key: fileName,
    Expires: 60,
    ContentType: fileType,
    ACL: 'public-read'
  };

  s3.getSignedUrl('putObject', s3Params, (err, data) => {
    if(err){
      console.log(err);
      return res.end();
    }
    const returnData = {
      signedRequest: data,
      url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
    };
    res.write(JSON.stringify(returnData));
    res.end();
  });
});

“过期”参数使签名的URL有效60秒。

我认为当签名的URL在上传过程中过期时,请求就会崩溃(尽管在上传开始时它是有效的)。

它并不是在60秒之后崩溃,而是随机地在60到120秒之间崩溃。大多数情况下,客户端会记录ERR_CONNECTION_RESET,而其他时候则会被禁止记录403。

在把它转到3600之后,我再也没有问题了。

我怀疑这个问题并没有发生在EC2s上,因为他们的上传速度非常快。

票数 5
EN

Stack Overflow用户

发布于 2014-05-21 20:26:57

我终于放弃了让这个开始工作。相反,我现在使用精巧的Uploader来提供这个功能。

票数 1
EN

Stack Overflow用户

发布于 2014-10-17 04:23:30

我想这个问题是没有决定的。

尝试一个POST请求:https://aws.amazon.com/articles/1434

代码语言:javascript
运行
复制
<form action="https://s3-bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
  <input type="hidden" name="key" value="uploads/${filename}">
  <input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY"> 
  <input type="hidden" name="acl" value="private"> 
  <input type="hidden" name="success_action_redirect" value="http://localhost/">
  <input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
  <input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
  <input type="hidden" name="Content-Type" value="image/jpeg">
  <!-- Include any additional input fields here -->

  File to upload to S3: 
  <input name="file" type="file"> 
  <br> 
  <input type="submit" value="Upload File to S3"> 
</form> 

如果您喜欢AJAX发送,请使用FormData追加字段

在nodejs:亚马逊S3 POST api,并与NodeJS签署策略中签名请求

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22798774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档