node.js - 如何通过 node.js 将上传流式传输到 S3 并向用户显示进度?

标签 node.js amazon-s3 upload jquery-file-upload busboy

目标是:

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

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

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

最佳答案

在前端 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);
});

关于node.js - 如何通过 node.js 将上传流式传输到 S3 并向用户显示进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793263/

相关文章:

laravel - 使用 laravel 将文件存储到 aws s3

python - 使用 Boto3 在 Django 上上传

node.js - 相当于 Maven 的神器的 Node 存储库是什么?

javascript - 带有构造函数和其他对象的 Module.exports

node.js - 如何优化 Promise 以避免回调/Promise hell

asp.net - SharePoint 文件大小限制

在 liferay 中上传大量文档文件时,Linux 服务器的内存不会释放

javascript - 避免使用 eval() 仅使用动态字符串按名称调用函数

Python AWS Boto3 : How to read files from S3 bucket?

amazon-web-services - 有人可以用硬编码的 "AWS-cognito-identity-poolID"侵入我的 s3 吗?