0

I am using first-time AWS' S3 bucket. I used node, express server, multer, and multerS3. For testing I used postman. I wanted to upload image to my s3 bucket. I have created the bucket also add my credentials to my backend. But when I am trying to upload an image by using postman, (this is how I did post request). I got error "TypeError: Cannot read property 'transfer-encoding' of undefined".

This is my s3 setup

const aws = require("aws-sdk");

const multer = require("multer");
const multerS3 = require("multer-s3");

aws.config.update({
  secretAccessKey: "AKIAJWFJ6GS2*******",
  accessKeyId: "W/2129vK2eLcwv67J******",
  region: "us-east-1"
});

const s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: "testing-alak",
    metadata: function(req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    },
    key: function(req, file, cb) {
      cb(null, Date.now().toString());
    }
  })
});

module.exports = upload;

This is upload file setup

const express = require("express");
const router = express.Router();

const upload = require("./upload-file");

const singleUpload = upload.single("image");

router.post("/", (req, res) => {
  singleUpload((req, res, next) => {
    return res.json({
      imgUrl: req.file.location
    });
  });
});

module.exports = router;

This is my express server

const express = require("express");
const app = express();
const route = require("./route");
const bodyParser = require("body-parser");
app.use(express.json()); //body Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/img", route);

const port = process.env.PORT || 5000;
app.listen(port, () => console.log(` App is listening at port ${port}!`));
juha
  • 515
  • 5
  • 10
  • 18

1 Answers1

0

If singleUpload is multer middleware, I've always used it like this:

router.post("/", singleUpload, (req, res) => {
  return res.json({
    imgUrl: req.file.location // this should be path?
  });
});

Also, I don't think there is a location property. Maybe path is what you are looking for?

fieldname       Field name specified in the form    
originalname    Name of the file on the user's computer 
encoding        Encoding type of the file   
mimetype        Mime type of the file   
size            Size of the file in bytes   
destination      The folder to which the file has been saved    DiskStorage
filename        The name of the file within the destination DiskStorage
path            The full path to the uploaded file  DiskStorage
buffer          A Buffer of the entire file MemoryStorage
djs
  • 3,947
  • 3
  • 14
  • 28