Dropbox JS mass writeFile 不写入所有文件

Dropbox JS mass writeFile not writing all files

我有这段代码应该 运行 通过某个目录的每个子目录中的文件,在我的 Dropbox 中创建相同的目录(具有相同的路径),然后将文件上传到该目录我的保管箱。我正在使用 node-walk 插件。

//This runs for every file
walker.on('file', function(path, stats, next){
    //Path to where it should upload to on my Dropbox
    var uploadPath = (dbPath + datePath + (path + '/uOcME0OGzMf7G3h39INs' + stats.name).replace('tmp-62u3dPAStPa6upQUa4G4/', '')).split('uOcME0OGzMf7G3h39INs');

    //Path to the file that it's scanning
    var localPath = path + '/' + stats.name;

                //The path without file
    client.mkdir(uploadPath[0], function(error){
        if (error) { return error; }

        fs.readFile(localPath, function(error, data){
            if (error){
                logToFile('fs.readFile error: ' + error);
                console.error(error);
            }

            client.writeFile(uploadPath.join(''), data, function(error, stat){
                if (error) {
                    logToFile('writeFile error: ' + error);
                    console.error(error);
                }
            });
        });
    });

    next();
});

问题是为了上传文件,我必须多次 运行 脚本(30+ 次)才能上传大部分文件。但是即使这样也有一些文件没有上传。

这是我得到的错误。有时当我 运行 脚本没有错误时,但下一次有。即使没有错误,所有文件都不存在。如果可能的话,我想阻止它出现。

Dropbox API error 503 from POST https://api-content.dropbox.com/1/files_put/auto/dropbox/path/for/file/here.css :: {"error": "Failed to grab locks for 871742009, please re-issue request."}

我可以做些什么来一次性上传所有文件?

发生此错误是因为您正在启动大量并行上传。有些成功是因为他们在服务器上排队并被阻止,但如果您发送多个并发请求,很可能有些人会在等待轮到他们时超时。

您需要按顺序上传,一次上传一个(或至少一次上传少量)以避免此类错误。

EDIT: 你想要的代码大概是这样的。 (我根本没有测试过。)我删除了对 mkdir 的调用,因为目录是在 Dropbox 中隐式创建的,但重要的变化是对 next() 的调用的位置。 (我假设 walker 是如何工作的,但我不知道它的行为方式。如果这仍然不起作用,您可能需要在调用 writeFile 时记录一些内容以查看何时它正在被调用。)

//This runs for every file
walker.on('file', function (path, stats, next) {
    //Path to where it should upload to on my Dropbox
    var uploadPath = (dbPath + datePath + (path + '/uOcME0OGzMf7G3h39INs' + stats.name).replace('tmp-62u3dPAStPa6upQUa4G4/', ''));

    //Path to the file that it's scanning
    var localPath = path + '/' + stats.name;

    fs.readFile(localPath, function (error, data) {
        if (error) {
            logToFile('fs.readFile error: ' + error);
            console.error(error);
        }

        client.writeFile(uploadPath, data, function (error, stat) {
            if (error) {
                logToFile('writeFile error: ' + error);
                console.error(error);
            }
            next();
        });
    });
});