我正在尝试使用node.js上的knox将八位字节流直接流到S3。八位字节流是从浏览器上传的XHR文件。我以为我可以将请求流式传输到putStream中,并且一切都可以正常工作,但是可惜没有。
这是我的代码:
var client = knox.createClient({
// AWS credentials here
});
if (req.headers['content-type'].match(/application\/octet-stream/i)) {
var filename = '/'+req.headers['x-file-name'];
client.putStream(req, filename, function(err, res){
// TODO: Catch errors
body = '{"success":"true"}'
res.writeHead(200,
{ 'Content-Type':'text/html'
, 'Content-Length':body.length
})
res.end(body)
});
}
和我收到的错误:
TypeError: Bad argument
at Object.stat (fs.js:354:11)
at Client.putStream (./lib/knox/client.js:181:6)
最佳答案
我正在做这样的事情:
app.post('/uploadAmazon', function(req, res) {
var params = req.query;
var request = client.request("PUT", '/' + req.header('x-file-name') + '?partNumber=' + params.partNumber
+ '&uploadId=' + params.uploadId, {
'Content-Length' : req.header('Content-Length')
} );
req.on('data', function(data){
request.write(data);
});
request.on('response', function(response) {
console.log('Partial ' + params.partNumber + ' statusCode: ' + response.statusCode);
if (response.statusCode== 200) {
uploadMap[params.id].currentSize++;
uploadMap[params.id].completeXmlArray[+(params.partNumber) - 1] = '<Part><PartNumber>' + params.partNumber + '</PartNumber><ETag>' + response.headers.etag + '</ETag></Part>' ;
if (uploadMap[params.id].currentSize == uploadMap[params.id].totalSize) {
uploadMap[params.id].uploadId = params.uploadId;
completeSend(uploadMap[params.id]);
}
}
}).end();
res.end();
});
假设我从帖子中收到了文件名,部件号和uploadId。