如何使用node.js,Express和knox从浏览器上传文件到Amazon S3?

我试图find一些利用node.js,Express和knox的示例代码。

Knox的文档只给出了如何上传已存储在文件系统中的文件的清晰示例。 https://github.com/learnboost/knox#readme

此外,还有一些简单的教程(即使是在Express中),也可以直接上传文件来保存到文件系统。

我遇到的麻烦是一个例子,它允许您将客户端上传到节点服务器,并将数据直接传输到S3,而不是先存储在本地文件系统中。

有人可以指出我的一个要点或其他包含这种信息的例子吗?

所有以前的答案都涉及到通过您的node.js服务器的上传,这是低效率和不必要的。 您的节点服务器无需处理带宽或上传文件的处理,因为Amazon S3允许直接从浏览器上传。

看看这个博客文章: http : //blog.tcs.de/post-file-to-s3-using-node/

我没有尝试在那里列出的代码,但看了看,它似乎坚实,我会试图执行它不久,广告将更新这个答案与我的发现。

这里是一个直接使用多方和knox直接传输到S3的例子,

var http = require('http') , util = require('util') , multiparty = require('multiparty') , knox = require('knox') , Batch = require('batch') , PORT = process.env.PORT || 27372 var s3Client = knox.createClient({ secure: false, key: process.env.S3_KEY, secret: process.env.S3_SECRET, bucket: process.env.S3_BUCKET, }); var Writable = require('readable-stream').Writable; util.inherits(ByteCounter, Writable); function ByteCounter(options) { Writable.call(this, options); this.bytes = 0; } ByteCounter.prototype._write = function(chunk, encoding, cb) { this.bytes += chunk.length; cb(); }; var server = http.createServer(function(req, res) { if (req.url === '/') { res.writeHead(200, {'content-type': 'text/html'}); res.end( '<form action="/upload" enctype="multipart/form-data" method="post">'+ '<input type="text" name="path"><br>'+ '<input type="file" name="upload"><br>'+ '<input type="submit" value="Upload">'+ '</form>' ); } else if (req.url === '/upload') { var headers = { 'x-amz-acl': 'public-read', }; var form = new multiparty.Form(); var batch = new Batch(); batch.push(function(cb) { form.on('field', function(name, value) { if (name === 'path') { var destPath = value; if (destPath[0] !== '/') destPath = '/' + destPath; cb(null, destPath); } }); }); batch.push(function(cb) { form.on('part', function(part) { if (! part.filename) return; cb(null, part); }); }); batch.end(function(err, results) { if (err) throw err; form.removeListener('close', onEnd); var destPath = results[0] , part = results[1]; var counter = new ByteCounter(); part.pipe(counter); // need this until knox upgrades to streams2 headers['Content-Length'] = part.byteCount; s3Client.putStream(part, destPath, headers, function(err, s3Response) { if (err) throw err; res.statusCode = s3Response.statusCode; s3Response.pipe(res); console.log("https://s3.amazonaws.com/" + process.env.S3_BUCKET + destPath); }); part.on('end', function() { console.log("part end"); console.log("size", counter.bytes); }); }); form.on('close', onEnd); form.parse(req); } else { res.writeHead(404, {'content-type': 'text/plain'}); res.end('404'); } function onEnd() { throw new Error("no uploaded file"); } }); server.listen(PORT, function() { console.info('listening on http://0.0.0.0:'+PORT+'/'); }); 

取自https://github.com/superjoe30/node-multiparty/blob/master/examples/s3.js的示&#x4F8B;

node / express代码不能与nodejs v0.4.7一起使用

这里是nodejs v0.4.7的更新代码

 app.post('/upload', function (req, res) { // connect-form additions req.form.complete(function (err, fields, files) { // here lies your uploaded file: var path = files['upload-file']['path']; // do knox stuff here }); }); 

*更新*

截至2009年中期,亚马逊支持CORS,并且不再需要通过您的node.js服务器进行上传。 您可以直接将file upload到S3。


在“连接forms”模块的帮助下,您可以将file upload到服务器(通过普通的多部分FORM),然后处理S3的东西…

 <form action="/upload" method="POST" id="addContentForm" enctype="multipart/form-data"> <p><label for="media">File<br/><input type="file" name="media" /></label></p> <p><button type="submit">upload</button></p> </form> 

节点/快递代码:

 app.post('/upload', function (req, res) { // connect-form additions req.form.complete(function (err, fields, files) { // here lies your uploaded file: var path = files['media']['path']; // do knox stuff here }); }); 

您必须将以下行添加到应用程序configuration中:

 app.configure(function(){ // rest of the config stuff ... app.use(form({ keepExtensions: true })); // ... }); 

connect-stream-s3库可以将所有的表单file upload到S3,作为中间件的一部分,所以你不必自己做任何逻辑。 它需要express.bodyParser()才能正常工作,但是我正在开发一个版本,在将文件写入磁盘之前将文件直接传输到Amazon S3:

请让我知道你如何得到。 希望一旦你在你的页面处理器中,自己做这件事就没有什么麻烦了。 🙂

我做了这个上传直接从Jqueryfile upload插件到S3公开文件 – 它应该指向你在正确的方向。

https://gist.github.com/3995819