Docker 和 Multer 上传卷 ENOENT 错误
Docker and Multer upload volumes ENOENT error
第一个问题+这里是初级开发!
所以我的问题是:我正在开发一个 API whith nodejs/express + Docker 和 Multer 我想上传文件。
我尝试尽可能好地配置 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 所以如果我错过了一个文件或一些行来帮助你更好地理解,请告诉我。
谢谢!
我想通了,只是中间件的一个简单的路径错误(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)}`
);
},
});
第一个问题+这里是初级开发!
所以我的问题是:我正在开发一个 API whith nodejs/express + Docker 和 Multer 我想上传文件。 我尝试尽可能好地配置 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 所以如果我错过了一个文件或一些行来帮助你更好地理解,请告诉我。
谢谢!
我想通了,只是中间件的一个简单的路径错误(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)}`
);
},
});