2013-07-09 59 views
0

我遇到零字节流的问题。我正在调整图像大小并将其作为流上传到S3。如果我输出管道响应它显示正确。流和S3上传问题

// Fetch remote file 
var request = http.get('mybigfile.jpg', function(response) { 

    // Setup IM convert 
    var convert = spawn('convert', ['-', '-strip', '-thumbnail', 600, '-']); 

    // Pipe file stream to it 
    response.pipe(convert.stdin); 

    // Pipe result to browser - works fine 
    //convert.stdout.pipe(res); 


    // S3 requires headers 
    var headers = { 
    'content-type': response.headers['content-type'], 
    'x-amz-acl': 'public-read' 
    }; 

    // Upload to S3 
    var aws = aws2js.load('s3', aws.key, aws.secret); 
    aws.setBucket(aws.bucket); 
    aws.putStream('thumb.jpg', convert.stdout, false, headers, function(err) { 
    if (err) { 
     return console.error('Error storing:', err.toString()); 
    } else { 
     // No errors - this shows - but file is 0kb 
     console.log(path + ' uploaded to S3'); 
    } 
    } 

我看到关于由于内容长度而不能使用S3的流的说明。我正在尝试缓冲区,但迄今为止没有成功。

回答

0

好吧,不要去流 - 我想我可以使用暂停流或多部分技术上实现这一点,但否则我不认为这是可能的。我结束了使用缓冲区。

... 
// Pipe file stream to it 
response.pipe(convert.stdin); 

// Save to buffer 
var bufs = [] ; 
convert.stdout.on('data', function(chunk) { 
    bufs.push(chunk); 
}); 
convert.stdout.on('end', function() { 
    var buffer = Buffer.concat(bufs); 

// S3 requires headers 
... 

aws.putBuffer(path, buffer, false, headers, function(err) { 
...