【问题标题】:Uploading large video file to s3 with multer-s3使用 multer-s3 将大型视频文件上传到 s3
【发布时间】:2019-10-26 10:16:33
【问题描述】:

我无法将视频等大文件上传到 s3。它最终超时。我曾尝试使用 fs 对其进行流式传输,但我一定不能正确使用它。

我已经尝试了所有我能想到的让 fs 流式传输此文件的方法。我不知道是否可以像我在 multerS3 中使用 fs 那样在单独的上传路径中使用它。我可以上传图片和非常小的视频,但仅此而已。

// Here is my s3 index file which exports upload

const crypto = require('crypto');
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const fs = require('fs');

aws.config.update({
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  region: 'us-east-1',
  ACL: 'public-read'
});

const s3 = new aws.S3({ httpOptions: { timeout: 10 * 60 * 1000 }});
var options = { partSize: 5 * 1024 * 1024, queueSize: 10 };

const fileFilter = (req, file, cb) => {
  console.log('file.mimetype is ', file.mimetype);
  if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' || file.mimetype === 'video/mp4' || file.mimetype === 'video/avi' || file.mimetype === 'video/mov' || file.mimetype === 'video/quicktime') {
    cb(null, true);
  } else {
    cb(new Error('Invalid file type'), false);
  }
}
const filename = getFileName();

const upload = multer({
  fileFilter,
  storage: multerS3({
    acl: 'public-read',
    s3,
    options,
    body: fs.createReadStream(filename),
    bucket: 'skilljack',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: 'TESTING_METADATA'})
    },
    key: function (req, file, cb) {
        let buf = crypto.randomBytes(16);
        buf = buf.toString('hex');
        let uniqFileName = file.originalname.replace(/\.jpeg|\.jpg|\.png|\.avi|\.mov|\.mp4/ig, '');
        uniqFileName += buf;
      cb(undefined, uniqFileName );
    }
  })
});

function getFileName (req, file) {
  if (file) {
    const body = fs.createReadStream(file.originalname);
    return body;
  }
}

  module.exports = {
      upload
  }

// Here is my route file
const express = require('express');
const router = express.Router({ mergeParams: true });
const multer = require('multer');
const { upload } = require('../s3');
const { asyncErrorHandler, isLoggedIn, isAuthor } = require('../middleware');


const {
    postCreate,
    postDestroy
} = require('../controllers/posts');

router.post('/', isLoggedIn, asyncErrorHandler(isAuthor), upload.single('image'), asyncErrorHandler(postCreate));
router.delete('/:post_id', isLoggedIn, asyncErrorHandler(isAuthor), asyncErrorHandler(postDestroy));
module.exports = router;

【问题讨论】:

  • 解决了吗?
  • 我无法让它与 multerS3 一起使用。我可以通过在本地上传文件然后将其分块加载到 S3 来使其工作。我使用 Agenda 将其作为单独的作业运行,并使用 s3.upload 上传到 S3 并使用 fs 读取传入文件。我让它工作的另一种方法是通过 S3 直接从浏览器加载文件,但是我无法让它以块的形式上传,所以我使用了第一种方法。第二种方式,一切都在 S3 结束时完成。

标签: node.js amazon-s3 file-upload fs multer-s3


【解决方案1】:

除了setting the queueSize to 1,您可能还想禁用超时。

const s3 = new aws.S3({
    accessKeyId: config.get('accessKeyId'),
    secretAccessKey: config.get('secretAccessKey'),
    Bucket: config.get('bucket'),
});

s3.config.httpOptions.timeout = 0

Reference

【讨论】:

    【解决方案2】:

    几天来一直有同样的问题。避免它的一种方法(但需要很长时间)是将 queueSize 减小到 1。

    当您的网络不好时会发生这种情况,最终,某些队列将保持空闲并触发超时。

    QueueSize 为 1 将允许为正在上传的部分分配更多带宽,避免超时。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2019-10-31
      • 2020-12-27
      • 2013-10-26
      • 2017-05-22
      • 2013-06-03
      • 2017-03-22
      • 2021-10-04
      • 2018-09-30
      相关资源
      最近更新 更多