上传文件似乎是可行的,即使在Done((被触发并创建了文件描述符时也是如此,但当我需要检索文件时,它并不存在,并给出以下错误:
Error: FileNotFound: file b33b78d2-30ea-4fe3-9228-2a8a7ccc8a77.PNG was not found
at /home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/gridfs-stream/download.js:284:17
at executeCallback (/home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/operations/execute_operation.js:70:5)
at handleCallback (/home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/utils.js:128:55)
是由节点v14引起的,如果我转向v12,它可以工作,但我不能使用它,因为它必须与Angular9实例共存。
所以我附上了我用来上传和下载的代码,以防万一,但它没有秘密,在节点v12:上工作了几个月
下载:
download: async function (req, res) {
var blobAdapter = require('skipper-gridfs')({
uri: 'mongodb://root:pASSWORD@127.0.0.1/files'
});
var fileID = req.param('id');
const fileModel = await Files.findOne({id: fileID});
blobAdapter.read(fileModel.fd, function(error , file) {
if(error) {
console.log(error);
res.json(error);
} else {
res.contentType(fileModel.contentType);
res.send(new Buffer(file));
}
});
}
上传:
uploadFile: function (req, res) {
const budgetID = req.param('id');
const scope = req.param('scope');
req.file('file').upload({
// don't allow the total upload size to exceed ~10MB
maxBytes: 2 * 10000000,
adapter: require('skipper-gridfs'),
uri: 'mongodb://root:pASSWORD@127.0.0.1/files'
}, async function whenDone(err, uploadedFiles) {
if (err) {
return res.serverError(err);
}
// If no files were uploaded, respond with an error.
if (uploadedFiles.length === 0){
return res.badRequest('No file was uploaded');
}
var file = await Files.create({
fd: uploadedFiles[0].fd,
name: uploadedFiles[0].filename,
contentType: uploadedFiles[0].type,
scope: scope,
groupID: req.session.userData.group,
userID: req.session.userData.id
}).fetch();
await Budget.addToCollection(budgetID, 'files').members([file.id]);
return res.ok(file);
});
}};
经过一番研究,我们尝试了AmazonS3 buckets,它是SailsJS连接器(skipper-S3(。完美地结合在一起,我们认为在我们的情况下这是一个更好的选择,所以我们排除了skipper-gridfs
,值得我们获得更好的解决方案。
现在,几个月后,skipper-gridfs
似乎将被修复,允许与Node v14兼容。目前,只有达尼·梅迪纳(Dani Medina(的队长格里德斯(gridfs(,而他的PR(44号和45号(没有合并到官方套餐中。
来自我的官方github问题的信息