Ответ загрузки файла корзины Amazon S3 недоступен

Я использую пакеты multer, aws-sdk и multer-s3 вместе с Express.

когда пользователь редактирует профиль, пользователь может изменить изображение профиля или аватар или нет.

Я передал объект Multer

multer({storage: multer.memoryStorage()}).single('profileHeroImageEdit')

если файл с текущим запросом, то я буду загружать файл в корзину s3, но я не получаю никакого ответа от upload_replace, где req.file.location предоставит URL-адрес корзины S3 (местоположение файла). И внутри upload_replace я могу получить файл, который я пытаюсь загрузить (req.file), но я хочу, чтобы местоположение загруженного файла в корзину S3.

Что мне не хватает? Помощь будет оценена

router.put("/:id",multer({ storage: 
multer.memoryStorage()}).single('profileHeroImageEdit'), 
middleware.checkProfileOwnership,function(req, res){
    if(isFileExists(req)==false){
        delete req.body.profileHeroImage      

     }
     else{
         console.log('file has')
         var upload_replace = multer({
            limits:{
                fileSize:MAX_FILE_SIZE,
                files:1
            },
            storage: multerS3({
                s3: photoBucket,
                bucket: BucketName,
                acl: 'public-read',
                metadata: function (req, file, cb) {
                    cb(null, { fieldName: file.fieldname });
                },
                key: function (req, file, cb) {
                    cb(null,Date.now().toString())

                }
            })
        }).single('profileHeroImageEdit') 

        upload_replace(req, res, function (err,log) {
           console.log('request log')
            console.log(req.file.location)
           console.log()
        }); 


     }
    Profile.findByIdAndUpdate(req.params.id, req.body.profile, function(err, updatedProfile){
        if (err){
            res.redirect("/profiles");
        } else {
            res.redirect("/profiles/" + req.params.id);
        }
    });
});

function isFileExists(request){
    if(request.file)
    {
        return true 
    }
    else{
        return false
    }
}

1 ответ

У меня есть весь код, используя multer и aws-sdk

  1. включите эти файлы и npm установите все

    //aws s3 packages
    const aws = require("aws-sdk");
    const multerS3 = require("multer-s3");
    const multer = require("multer");
    const path = require("path");
    
  2. затем

    //profile image upload start
    const s3 = new aws.S3({
      accessKeyId: "***",
      secretAccessKey: "***",
      Bucket: "***"
    });
    
    //Singe profile image upload
    
    const profileImgUpload = multer({
      storage: multerS3({
        s3: s3,
        bucket: "***",
        acl: "public-read",
        key: function(req, file, cb) {
          cb(
            null,
            path.basename(file.originalname, path.extname(file.originalname)) +
              "-" +
              Date.now() +
              path.extname(file.originalname)
          );
        }
      }),
      limits: { fileSize: 2000000 }, // In bytes: 2000000 bytes = 2 MB
      fileFilter: function(req, file, cb) {
        checkFileType(file, cb);
      }
    }).single("profileImage");
    
    // getExtension of file by this function
    function getExtension(filename) {
      var parts = filename.split(".");
      return parts[parts.length - 1];
    }
    
    //checkfile type of input function
    function checkFileType(file, cb) {
      const ext = getExtension(file.originalname);
      switch (ext.toLowerCase()) {
        case "jpeg":
        case "jpg":
        case "png":
        case "gif":
          return cb(null, true);
      }
      cb("Error: Images Only!");
    }
    
    router.post(
      "/image",
      passport.authenticate("jwt", { session: false }),
      (req, res) => {
        profileImgUpload(req, res, error => {
          if (error) {
            res.json({ error: error });
          } else {
            //here we can get req.body
            const userDp = {};
    
            //end of getting values
    
            // If File not found then dont store anything
            if (req.file !== undefined) userDp.dpUrl = req.file.location;
            // Save the file name into database into profile model
    
            User.findOne({ email: req.user.email }).then(user => {
              if (user) {
                // Update
                User.findOneAndUpdate(
                  { email: req.user.email },
                  { $set: userDp },
                  { new: true }
                ).then(user => res.json(user));
              } else {
                res.json({ msg: "not able not update data" });
              }
            });
          }
        });
      }
    );
    

3. Нужно отправить данные из реагирующего интерфейса с помощью

const data = new Formdata();
data.append()

и включая заголовки также

Добавьте слушателя, когда OutgoingMessage для загрузки фотографии на S3 завершен. on-finished Библиотека удобна для этого.

const onFinished = require('on-finished');
const print = process._rawDebug;

uploadReplace(req, null, function (err) {
  onFinished(req, function () {
    if (err) print(err);
    print(req.file.location);
    // Other things can be done here
  })
});
Другие вопросы по тегам