when Node uploads multiple files (no more than 100k for JS and CSS, each) to Qiniu, very few of them upload successfully, and most of them always return "connect ETIMEDOUT" at the 21st second (and sometimes return "write ECONNRESET" and "read ECONNRESET")
the code is as follows:
var fs = require("fs");
var qiniu = require("qiniu");
var accessKey = "********";
var secretKey = "********";
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var staticPath = "C:/files/";
var prefix = "static";
var bucket = "static";
var config = new qiniu.conf.Config();
config.zone = qiniu.zone.Zone_z1;
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
putExtra = null;
//
function uploadFile(localFile) {
//
const key = localFile.replace(staticPath, prefix)
const options = {
scope: bucket + ":" + key,
returnBody: "{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}"
}
const putPolicy = new qiniu.rs.PutPolicy(options)
//
const uploadToken = putPolicy.uploadToken(mac)
//
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr, respBody, respInfo) {
if (respErr) {
throw respErr
}
if (respInfo.statusCode == 200) {
} else {
console.log(respInfo.statusCode);
console.log(respBody);
}
})
}
//
function uploadDirectory(dirPath) {
fs.readdir(dirPath, function (err, files) {
if (err) throw err
//
files.forEach(item => {
let path = `${dirPath}/${item}`
fs.stat(path, function (err, stats) {
if (err) {
throw err
}
//
if (stats.isDirectory()) uploadDirectory(path)
else uploadFile(path, item)
})
})
})
}
fs.exists(staticPath, function (exists) {
if (!exists) {
console.log("")
} else {
console.log("...")
uploadDirectory(staticPath)
}
})
fs reads all the files under the folder, and then iteratively calls the putFile method to upload. It always reports an error when uploading to 21 seconds:
the file missed was not uploaded successfully. May I ask what caused it, how to solve it, and why Node will not quit automatically after the program is completed?