0

我第一次使用 AWS 的 S3 存储桶。我使用了 node、express server、multer 和 multerS3。为了测试,我使用了邮递员。我想将图像上传到我的 s3 存储桶。我创建了存储桶并将我的凭据添加到我的后端。但是当我尝试使用邮递员上传图片时,(这就是我发布请求的方式)。我收到错误“TypeError:无法读取未定义的属性‘传输编码’”。

这是我的 s3 设置

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;

这是上传文件设置

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;

这是我的快递服务器

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}!`));
4

1 回答 1

0

如果singleUploadmulter中间件,我一直是这样使用的:

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

另外,我认为没有location财产。也许path是你正在寻找的?

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
于 2020-04-06T06:55:51.287 回答