1

Everything I've looked at regarding s3 and multer s3 looks something like this

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'bucket',
    acl: 'public-read',
    cacheControl: 'max-age=31536000',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    },
    key: function (req, file, cb) {
      const key = `pictures/${Date.now().toString()}`;
      cb(null, key);
    },
  }),
});

and then the controller action looks like this

 app.post('/profileImage', upload.single('profileImage'), async function (req, res) {
    const { _id } = req.params;
    const user = await User.findOneAndUpdate({ _id }, { image: req.file.location }, { new: true });
    res.json(user);
  });

My question is can I send back form data and other data in one call and upload my images to s3. I want to be able to structure my data something like this

const formData = new FormData();
        formData.append("picture", this.file);
        const data = {
          title: this.toDoTitle,
          details: this.toDoDetails,
          formData,
        };
        axios
          .post("/profileImage", data)
          .then(() => {
          
          })
          .catch(err => {
            console.log(err);
          });

and then in my controller be able to save that form data image in s3 but also interact with the other data. I've been able to send back data in the req.params but I want to know if you can send it back in the body as well with the image and be able to upload image and get other data

Aaron Angle
  • 121
  • 2
  • 6

0 Answers0