我只是尝试按照本教程学习如何使用Node和Express将文件上传到gcs。但是以下错误一直导致我的应用程序崩溃。通常,我可以在第一次运行时毫无问题地上传一个文件。但是运行一些请求后,即使使用不同的文件,我也会收到此错误。当我尝试一次上传 5 个文件时,此错误会导致我的应用程序即使在第一次运行时崩溃。我看到该过程正在尝试重命名 .config 文件夹中的文件。这是正常行为吗?如果是这样,是否有解决方法?
Window: v10.0.10586
Node: v4.3.1
Express: v4.13.1
Error: EPERM: operation not permitted, rename 'C:UsersJames Wang.configconfigstoregcs-resumable-upload.json.2873606827' -> 'C:UsersJames Wang.configconfigstoregcs-resumable-upload.json'
at Error (native)
at Object.fs.renameSync (fs.js:681:18)
at Function.writeFileSync as sync
at Object.create.all.set (C:UsersJames Wanggi-cms-backendnode_modulesconfigstoreindex.js:62:21)
at Object.Configstore.set (C:UsersJames Wanggi-cms-backendnode_modulesconfigstoreindex.js:93:11)
at Upload.set (C:UsersJames Wanggi-cms-backendnode_modulesgcs-resumable-uploadindex.js:264:20)
at C:UsersJames Wanggi-cms-backendnode_modulesgcs-resumable-uploadindex.js:60:14
at C:UsersJames Wanggi-cms-backendnode_modulesgcs-resumable-uploadindex.js:103:5
at Request._callback (C:UsersJames Wanggi-cms-backendnode_modulesgcs-resumable-uploadindex.js:230:7)
at Request.self.callback (C:UsersJames Wanggi-cms-backendnode_modulesrequestrequest.js:199:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request. (C:UsersJames Wanggi-cms-backendnode_modulesrequestrequest.js:1036:10)
at emitOne (events.js:82:20)
at Request.emit (events.js:169:7)
at IncomingMessage. (C:UsersJames Wanggi-cms-backendnode_modulesrequestrequest.js:963:12)
[nodemon] app crashed - waiting for file changes before starting...
更新:按照@stephenplusplus在这篇文章中的建议设置 {resumable: false} 后,我不再收到"EPERM:不允许操作"错误。但是,我开始遇到{[错误:ETIMEDOUT]代码:"ETIMEDOUT",连接:假}错误,同时尝试一次上传多个文件,最大文件大于1.5mb。其他文件上传成功。
有关更多信息,当文件不大于~2.5mb时,我可以逐个上传文件。如果我尝试一次上传 3 个文件,我只能使用不大于 ~1.5mb 的文件。
问题中指定的"不允许操作"问题是否是特定于窗口的事情,超时问题是否仅在我设置了 resumable = false 后才会发生?
我正在使用带有节点的表达式和 multer。
这是我现在使用的代码:
// Express middleware that will handle an array of files. req.files is an array of files received from
// filemulter.fields([{field: name, maxCount}]) function. This function should handle
// the upload process of files asychronously
function sendFilesToGCS(req, res, next) {
if(!req.files) { return next(); }
function stream(file, key, folder) {
var gcsName = Date.now() + file.originalname;
var gcsFile = bucket.file(gcsName);
var writeStream = gcsFile.createWriteStream({ resumable: false });
console.log(key);
console.log('Start uploading: ' + file.originalname);
writeStream.on('error', function(err) {
console.log(err);
res.status(501).send(err);
});
writeStream.on('finish', function() {
folder.incrementFinishCounter();
req.files[key][0].cloudStorageObject = gcsName;
req.files[key][0].cloudStoragePublicUrl = getPublicUrl(gcsName);
console.log('Finish Uploading: ' + req.files[key][0].cloudStoragePublicUrl);
folder.beginUploadNext();
});
writeStream.end(file.buffer);
};
var Folder = function(files) {
var self = this;
self.files = files;
self.reqFilesKeys = Object.keys(files); // reqFilesKeys is an array of keys parsed from req.files
self.nextInQuene = 0; // Keep track of the next file to be uploaded, must be less than reqFilesKeys.length
self.finishCounter = 0; // Keep track of how many files have been uploaded, must be less than reqFilesKeys.length
console.log(this.reqFilesKeys.length + ' files to upload');
};
// This function is used to initiate the upload process.
// It's also called in the on-finish listener of a file's write-stream,
// which will start uploading the next file in quene
Folder.prototype.beginUploadNext = function() {
// If there's still file left to upload,
if(this.finishCounter < this.reqFilesKeys.length) {
// and if there's still file left in quene
if(this.nextInQuene < this.reqFilesKeys.length) {
// upload the file
var fileToUpload = this.files[this.reqFilesKeys[this.nextInQuene]][0];
stream(fileToUpload, this.reqFilesKeys[this.nextInQuene], this);
// Increment the nextInQuene counter, and get the next one ready
this.nextInQuene++;
}
} else {
console.log('Finish all upload!!!!!!!!!!!!!!!!!!!!!!');
next();
}
};
Folder.prototype.incrementFinishCounter = function() {
this.finishCounter++;
console.log('Finished' + this.finishCounter + ' files');
};
var folder = new Folder(req.files);
// Begin upload with 3 streams
/*for(var i=0; i<3; i++) {
folder.beginUploadNext();
}*/
//Upload file one by one
folder.beginUploadNext();
}
我对鲍尔也有同样的问题..运行以下命令:鲍尔缓存清理 --allow-root
如果这不能解决问题,请在禁用防病毒软件后尝试。