从React Native Frontend解压缩视频文件,然后上传到s3存储桶

时间:2018-08-05 15:14:01

标签: javascript node.js react-native amazon-s3

我试图解压缩或解压缩在React Native前端(ios)上选择的视频文件,然后将其上传到s3存储桶,而没有任何临时文件夹保存在我的服务器上。我已经成功上传了照片,但是视频和文件显示在我的s3存储桶中,显然仍然被压缩且无法查看。这是服务器接收到的对象的控制台日志的屏幕截图。

Console Log Screenshot

这是相关的路线。

exports.uploadVideo = (req, res) => {
  const { clientKey } = req.params;
if (clientKey !== config.clientKey) {
    res.send({ message: 'You do not have the correct client key to access this 
database.'})
} else {
console.log(req.files.video);
console.log(req.body);
if(req.body.status == 'Free' && +req.body.storage+req.files.video.size > 640000000) {
  res.send({ message: 'You have exceeded the size limit for your account.'})
} if(req.body.status == 'Subscription' && +req.body.storage+req.files.video.size > 1280000000) {
  res.send({ message: 'You have exceeded the size limit for your account.'})
} else {

  const element1 = req.query.userId;
  console.log(req.files.video);
   var busboy = new Busboy({ headers: req.headers });
   // The file upload has completed
   busboy.on('finish', function() {

    const athleteVideo = {
      video: req.files.video,
      userId: element1
    };
    let s3bucket2 = new AWS.S3({
      accessKeyId: IAM_USER_KEY,
      secretAccessKey: IAM_USER_SECRET,
      Bucket: VIDEO_BUCKET_NAME,
    });
    s3bucket2.createBucket(function () {
      var params = {
       Bucket: VIDEO_BUCKET_NAME,
       Key: `${athleteVideo.userId}/${athleteVideo.video.name}`,
       Body: athleteVideo.video.data,
      };
      s3bucket2.upload(params, function (err, data) {
       if (err) {
        console.log('error in callback');
        res.send(err);
       }
       console.log('success');

       User.findById({ _id: req.query.userId })
        .then(user => {

          user.athleteProfile.storage = user.athleteProfile.storage+req.files.video.size;

          let newMedia = {
            title: req.body.title,
            description: req.body.description,
            created_timestamp: new Date,
            location: data.Key,
            size: req.files.video.size
          };

          console.log(newMedia);

          if(user.athleteProfile.media == undefined) {
            user.athleteProfile.media = [ newMedia ];
          } else {
            user.athleteProfile.media.push(newMedia);
          }

          user.save((err, newUser) => {
            if (err) {
              console.log(err);
            } else {
              console.log(newUser);
              res.status(200).send({newUser});
            }
          });
        })
        .catch(err => {
          console.log(err);
        })
      });
    });
   });
     req.pipe(busboy);
   }
 }
}

请清楚一点,我知道上面的代码中没有任何文件在将文件发送到s3之前将其解压缩。我只是想显示我现在正在将压缩文件上传到s3的工作。

这些是我已经发现的最相关的问题,它们要么太旧,要么不能完全解决我的问题。我已尽我所能尝试了所有这三个选项。 -Nodejs extract file from ZIP -File upload in Node JS -S3 file upload stream using node js

我在这里问问题还很陌生,所以如果我需要澄清任何事情,请告诉我。

0 个答案:

没有答案