2

我正在尝试使用带有以下代码(https://github.com/thaume/s3-streaming)的 connect-busboy 将文件从浏览器直接上传到 Amazon S3

路由 - index.js: var awsUpload = require('../services/aws-streaming');

// Index route
// ===========
exports.index = function(req, res){
  if (req.method === 'POST') {
    return awsUpload(req, function(err, url) {
      res.send(JSON.stringify(url));
    });
  }

  res.writeHead(200, { Connection: 'close' });
  res.end('<html><head></head><body>\
             <form method="POST" enctype="multipart/form-data">\
              <input type="file" name="filefield"><br />\
              <input type="submit">\
            </form>\
          </body></html>');
};

使用我修改后的 aws-streaming.js 版本

// Initialize aws client
// =====================
var config = require('../config/' + 'development');
var Knox = require('knox');
var moment = require('moment');
var crypto = require('crypto');

// Create the knox client with your aws settings
Knox.aws = Knox.createClient({
  key: config.aws.AWS_ACCESS_KEY_ID,
  secret: config.aws.AWS_SECRET_ACCESS_KEY,
  bucket: config.aws.S3_BUCKET_NAME,
  region: 'eu-west-1'
});

// S3 upload service - stream buffers to S3
// ========================================
var s3UploadService = function(req, next) {
  req.files = {};

  req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    if (!filename) {
      // If filename is not truthy it means there's no file
      return;
    }
    //////////////// CHECK FOR MIMETYE /////////////////////
    // If file is not "text/plain" - return //
    if (mimetype != "text/plain") {
      console.log('true!')
      return;  // A JSON array with an error "Wrong file type"!
    }
    // Create the initial array containing the stream's chunks
    file.fileRead = [];

    file.on('data', function(chunk) {
      // Push chunks into the fileRead array
      this.fileRead.push(chunk);
    });

    file.on('error', function(err) {
      console.log('Error while buffering the stream: ', err);
    });

    file.on('end', function() {
      // Concat the chunks into a Buffer
      var finalBuffer = Buffer.concat(this.fileRead);

      req.files[fieldname] = {
        buffer: finalBuffer,
        size: finalBuffer.length,
        filename: filename,
        mimetype: mimetype
      };

      // Generate date based folder prefix
      var datePrefix = moment().format('YYYY[/]MM');
      var key = crypto.randomBytes(10).toString('hex');
      var hashFilename = key + '-' + filename;

      var pathToArtwork = '/artworks/' + datePrefix + '/' + hashFilename;

      var headers = {
        'Content-Length': req.files[fieldname].size,
        'Content-Type': req.files[fieldname].mimetype,
        'x-amz-acl': 'public-read'
      };

      Knox.aws.putBuffer( req.files[fieldname].buffer, pathToArtwork, headers, function(err, response){
        if (err) {
          console.error('error streaming image: ', new Date(), err);
          return next(err);
        }
        if (response.statusCode !== 200) {
          console.error('error streaming image: ', new Date(), err);
          return next(err);
        }
        console.log('Amazon response statusCode: ', response.statusCode);
        console.log('Your file was uploaded');
        next();
      });
    });
  });

  req.busboy.on('error', function(err) {
    console.error('Error while parsing the form: ', err);
    next(err);
  });

  req.busboy.on('finish', function() {
    console.log('Done parsing the form!');
    // When everythin's done, render the view
    next(null, 'http://www.google.com');
  });

  // Start the parsing
  req.pipe(req.busboy);
};

module.exports = s3UploadService;

我想做的是验证 mimetype 并返回一个带有错误消息的 json 数组,结束对表单的解析并且不上传文件。已将代码添加到 aws-streaming,但即使验证为 true,它也不会返回。我做错了什么?

此外,代码在完成解析表单时运行回调,但我希望它在文件实际上传时运行。我该如何实现这一点,注释掉“完成”事件中的 next() 并将其移动到 Knox.aws.putBuffer?

我正在使用 Express 4

4

0 回答 0