1

目标是:

  1. 能够从浏览器上传文件
  2. 能够将上传直接流式传输到 S3
  3. 将上传进度中继到浏览器

我花了一段时间才弄清楚这一点(并且还没有完全找到在 StackOverflow 上完成所有 3 个问题的答案)所以发布我的答案以防其他人有同样的问题。

我愿意接受更好的解决方案的建议(我的速度非常可怕)。

4

1 回答 1

5

在前端 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);
});
于 2015-09-26T03:14:43.290 回答