你如何通过node.jsstream向S3上传并向用户显示进度?

目标是:

  1. 能够从浏览器上传文件
  2. 能够直接上传到S3
  3. 将上传进度转发给浏览器

我花了一段时间来解决这个问题(并没有完全find一个答案,所有3在StackOverflow呢),所以张贴我的答案,以防其他人有同样的问题。

我愿意提供更好的解决scheme(我的速度非常可怕)。

在前端HTML

<input type="file" class="file" name="files[]"> 

在前端JS

 require('blueimp-file-upload'); var socket = require('socket.io-client'); var fileSize; var socketId; // Socket will be used to transmit the percent uploaded from server socket.connect() // when the socket gives us a unique ID we will save it so that we // can identify the client .on('id', function (data) { socketId = data; }) .on('uploadProgress', function (data){ // you can show a progress bar with this percentage var pct = data.loaded / fileSize; }); $('.file') .change(function (){ // save the file size so that we can calculate perfectage fileSize = this.files[0].size; }) .fileupload({ dataType: 'json', }) .fileupload('option', { url: '/upload?socketId=' + socketId }); 

在服务器上

 var Busboy = require('busboy'); var AWS = require('aws-sdk'); var socket = require('socket.io'); var express = require('express'); var http = require('http'); // Set up Express var app = express(); var server = http .Server(app) .listen(80); var io = socket(server); // keeps track of all the open sockets var sockets = {}; io.on('connection', function (socket) { var uniqueId = _.random(0, 100000000); app.socket[uniqueId] = socket; socket.emit('id', uniqueId); }); // Set up upload route app.post('/upload', function (req, res) { AWS.config.update({ appKey: '', jsKey: '', }); var s3 = new AWS.S3(); var busboy = new Busboy({ headers: req.headers }); busboy.on('file', function(fieldname, file, filename) { s3.upload({ Bucket: 'bucketname', Key: new Date().getTime() + filename, Body: file //stream }, function(err, file){ res.json({ success: true }); }).on('httpUploadProgress', function(evt) { //emit progress sockets[req.query.socketId].emit('uploadProgress', evt); }); }); req.pipe(busboy); });