可读流在使用之前就失败了

时间:2015-09-09 06:49:52

标签: javascript node.js asynchronous coffeescript stream

我有以下问题。我有一个可读的流,我想写入s3桶。

在我将文件写入s3存储桶之前,我会执行HEAD请求以检查该文件是否已存在。一旦我从HEAD请求得到响应,我就开始使用上面提到的流上传。

事情是,当我从头部请求得到回复时,流结束,因此上传不按预期工作。

我已经尝试过直接运行PUT并按预期工作:)。

关于如何调试流为什么提前结束的任何想法? 当你让它们等待太久时,流是否常见?

示例代码(不起作用):

req.on "response", (response) =>
  # Analyse the head response and discard un-necessary information
  head_status = parseInt response.statusCode
  response.resume()

  #if head_status is 404 or Math.floor(head_status / 100) is 3 # Handle redirects
  console.log("HERE")
  logger.debug "S3 Multipart Upload", id

  upload = new MultiPartUpload {
    client: @s3
    objectName: id
    stream: stream
    headers: {'content-type': content_type}
    noDisk: true
  }, (err, body) ->
    console.log("Got to here!!!")
    if err?
      logger.debug "Unable to PUT stream to S3 for", id, err
      callback new Error(err)
      return
    else
      callback null, {success: true, id: id}

示例代码(有效)

upload = new MultiPartUpload {
        client: @s3
        objectName: id
        stream: stream
        headers: {'content-type': content_type}
        noDisk: true
      }, (err, body) ->
        console.log("Got to here!!!")
        if err?
          logger.debug "Unable to PUT stream to S3 for", id, err
          callback new Error(err)
          return
        else
          callback null, {success: true, id: id}

0 个答案:

没有答案
相关问题