【问题标题】:Docker and Multer upload volumes ENOENT errorDocker 和 Multer 上传卷 ENOENT 错误
【发布时间】:2022-01-18 22:15:44
【问题描述】:

这里是第一个问题 + 初级开发人员!

所以我的问题是:我正在开发一个带有 nodejs/express + Docker 和 Multer 的 API,我想在其中上传文件。 我尝试尽可能好地配置 Docker,与 Multer 一样,并将上传的文件保存在一个卷中,但它一直向我抛出这个错误:

{
    "errno": -13,
    "code": "EACCES",
    "syscall": "open",
    "path": "public/media/pictures/picture-1642414319690.jpg",
    "storageErrors": []
}

这是我的 Multer 上传中间件配置:

const multer = require('multer');
const path = require('path');

// PICTURES
// Picture storage path
const storage = multer.diskStorage({
  destination(req, file, cb) {
    cb(null, '/public/media/pictures');
  },
  filename: (req, file, cb) => {
    cb(
      null,
      `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
    );
  },
});
// Check pictures type
const checkPicType = (file, cb) => {
  // Allowed ext
  const pictypes = /jpeg|jpg|png/;
  // Check ext
  const extname = pictypes.test(path.extname(file.originalname).toLowerCase());
  // Check mime
  const mimetype = pictypes.test(file.mimetype);

  if (mimetype && extname) {
    return cb(null, true);
  }
  return cb('Error: Images only!');
};
// Picture upload options
const picUpload = multer({
  storage,
  limits: {
    fields: 5,
    fieldNameSize: 10,
    fieldSize: 2000,
    fileSize: 25000000,
  },
  fileFilter(req, file, cb) {
    checkPicType(file, cb);
  },
}).single('picture');

module.exports = {
  picUpload,
};

我在 api/picture.js 中的上传方法:

router.post('/upload', (req, res) => {
  picUpload(req, res, (err) => {
    if (err) {
      return res.status(403).json(err);
    }
    return res.status(201).json({
      path: `${req.protocol}://${req.hostname}:${PORT}/${req.file.path}`,
    });
  });
});

最后是我的 docker-compose :

services:
  web:
      build:
        context: ./
        target: dev
      volumes:
        - .:/src
        - uploaded-files:/src/public/media/files
        - uploaded-pictures:/src/public/media/pictures
      command: npm run start:dev
      ports:
        - "5000:5000"
      environment:
        NODE_ENV: development
        DEBUG: nodejs-docker-express:*
  postgres:
    image: postgres
    restart: always
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'

volumes:
    postgres:
    uploaded-files:
    uploaded-pictures:

正如我所说,我对 docker 和 multer 很友好,所以如果我遗漏了一个文件或一些有助于您更好理解的行,请告诉我。

谢谢!

【问题讨论】:

  • EACCES 表示您在尝试打开文件时没有(在您的情况下为读取)权限..

标签: javascript node.js docker express multer


【解决方案1】:

我想通了,这只是中间件(picUpload.js)中的一个简单路径错误

cb(null, '/src/public/media/pictures');

// Picture storage path
const storage = multer.diskStorage({
  destination(req, file, cb) {
    cb(null, '/src/public/media/pictures');
  },
  filename: (req, file, cb) => {
    cb(
      null,
      `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
    );
  },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2018-05-27
    • 2016-03-06
    • 2016-06-18
    • 2022-10-03
    • 2018-03-22
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多