1

我只是想按照本教程学习如何使用 Node 和 Express 将文件上传到 gcs。但是以下错误不断导致我的应用程序崩溃。通常,我可以在第一次运行时毫无问题地上传一个文件。但是在运行几个请求后我会得到这个错误,即使是不同的文件。当我尝试一次上传 5 个文件时,即使在第一次运行时,此错误也会导致我的应用程序崩溃。我看到该过程正在尝试重命名 .config 文件夹中的文件。这是正常行为吗?如果是这样,是否有解决方法?

Window: v10.0.10586 Node: v4.3.1 Express: v4.13.1

Error: EPERM: operation not permitted, rename 'C:\Users\James Wang.config\configstore\gcs-resumable-upload.json.2873606827' -> 'C:\Users\James Wang.config\configstore\gcs-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:\Users\James Wang\gi-cms-backend\node_modules\configstore\index.js:62:21) at Object.Configstore.set (C:\Users\James Wang\gi-cms-backend\node_modules\configstore\index.js:93:11) at Upload.set (C:\Users\James Wang\gi-cms-backend\node_modules\gcs-resumable-upload\index.js:264:20) at C:\Users\James Wang\gi-cms-backend\node_modules\gcs-resumable-upload\index.js:60:14 at C:\Users\James Wang\gi-cms-backend\node_modules\gcs-resumable-upload\index.js:103:5 at Request._callback (C:\Users\James Wang\gi-cms-backend\node_modules\gcs-resumable-upload\index.js:230:7) at Request.self.callback (C:\Users\James Wang\gi-cms-backend\node_modules\request\request.js:199:22) at emitTwo (events.js:87:13) at Request.emit (events.js:172:7) at Request. (C:\Users\James Wang\gi-cms-backend\node_modules\request\request.js:1036:10) at emitOne (events.js:82:20) at Request.emit (events.js:169:7) at IncomingMessage. (C:\Users\James Wang\gi-cms-backend\node_modules\request\request.js:963:12) [nodemon] app crashed - waiting for file changes before starting...

更新:按照@stephenplusplus 在这篇文章中的建议设置 {resumable: false} 后,我不再收到“EPERM:不允许操作”错误。但是,我开始遇到 { [ERROR:ETIMEDOUT] 代码:'ETIMEDOUT ', connection: false } 尝试一次上传多个文件时出错,最大文件大于 1.5mb。其他文件上传成功。

有关更多信息,当文件不大于 ~2.5mb 时,我可以一个一个地上传文件。如果我尝试一次上传 3 个文件,我只能上传不大于 ~1.5mb 的文件。

问题中指定的“不允许操作”问题是否是特定于窗口的问题,并且超时问题是否仅在我设置 resumable = false 后才会发生?

我正在使用带有节点的 express 和 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();
}
4

1 回答 1

0

我对 bower 有同样的问题 .. 运行以下命令: bower cache clean --allow-root

如果这不能解决问题,请在禁用防病毒后尝试。

于 2016-07-22T15:53:49.680 回答