Dropzone对接AWS S3上传10个以上文件时processQueue队列卡住求助
核心问题根因
- 你在
accept回调里只创建了ManagedUpload实例,既没有调用实例的send()方法实际启动S3上传,也没有拦截Dropzone自带的原生XHR上传流程。Dropzone的队列调度完全感知不到S3侧的上传状态,仅靠原生XHR的状态更新释放并发槽位,一旦XHR请求挂起,槽位就会被永久占用。 - 并发设置完全超出浏览器承载能力:
parallelUploads: 20的配置下,每个上传对应的ManagedUpload还会默认拆成多分片并发,单文件就能占3-4个浏览器同域连接(Chrome同域并发上限仅6-8个),20个并行任务会直接把所有连接占满,后续请求会一直排队挂起,表现就是队列中断。 accept回调存在流程漏洞:当getAwsToken()请求失败时,只弹出了错误提示,没有向done()函数传入错误标记,文件会永久卡在Dropzone的校验环节,既不进入上传队列,也不被移除,时间久了队列就会堵死。- 缺少超时和异常兜底:单个S3分片请求如果因为网络波动挂起,既没有自动超时重试,也不会主动上报失败,对应的上传任务会一直挂死,永远不触发队列释放。
可落地修复方案
1. 打通S3上传和Dropzone队列的状态联动
修改Dropzone的init配置,拦截原生XHR上传流程,将S3上传的全流程事件同步回Dropzone,确保不管上传成功还是失败,都能正确释放队列槽位:
init: function () { const dzInstance = this; // 拦截文件进入上传处理环节的事件 dzInstance.on('processing', function(file) { // 记录文件开始处理的时间,用于后续卡死检测 file.processingStartedAt = Date.now(); // 阻止Dropzone走默认XHR上传流程 dzInstance.emit("processingdone", file); // 启动S3 ManagedUpload file.s3upload.send({ // 同步上传进度到Dropzone UI onProgress(progress) { const progressBar = file.previewElement.querySelector("[data-dz-uploadprogress]"); if (progressBar) progressBar.style.width = `${progress.percent}%`; dzInstance.emit("uploadprogress", file, progress.percent, progress.total); }, // 上传成功后同步状态 onSuccess() { dzInstance.emit("success", file, {}, null); dzInstance.emit("complete", file); }, // 上传失败后同步状态,文件自动重入队列重试 onError(err) { dzInstance.emit("error", file, err); dzInstance.emit("complete", file); // 重置文件状态重新入队 file.status = 'queued'; dzInstance.enqueueFile(file); } }); }); // 保留原有移除文件时中断上传的逻辑 this.on('removedfile', function (file) { if (file.s3upload) file.s3upload.abort(); }); }
注意:必须在S3上传流程不管成功/失败时,都触发Dropzone的
complete事件,否则队列槽位永远不会释放。
2. 调整并发参数匹配浏览器限制
- 将Dropzone的
parallelUploads参数从20调整到3-5:这个参数控制Dropzone层面同时处理的文件数,过高会直接打满浏览器连接上限 - 优化
getManagedUpload的封装参数,控制单文件的分片并发和超时逻辑:
getManagedUpload: function (options) { return new AWS.S3.ManagedUpload({ ...options, // 单个分片设为5MB,避免分片过大 partSize: 5 * 1024 * 1024, // 单文件的分片并发队列设为3,不额外占用过多连接 queueSize: 3, // 单个请求超时设为30秒,挂起自动重试 httpOptions: { timeout: 30000 } }) }
3. 修复accept回调的流程漏洞
补全accept回调的异常分支逻辑,确保校验失败的文件能被Dropzone正确移除,不占用队列资源:
accept: function(file, done) { selector.getAwsHelper().getAwsToken().then(function(data) { // 原有S3上传参数构建逻辑保留 var fullKey = selector.getUtil().sanitize_path( selector.getAwsHelper().getUserFileKey(selector.settings.userId, selector.directory, file.name), true ); var params = { Bucket: selector.getAwsHelper().S3DefaultBucket, Key: fullKey, Body: file, Region: selector.getAwsHelper().region, ContentType: file.type }; file.s3upload = selector.getAwsHelper().getManagedUpload({params: params}); // 校验通过,通知Dropzone done(); }).catch(function(err) { console.error({ method: "ImageSelector::createDropzone", message: "Dropzone::accept file accept error" }, err); // 关键:校验失败时必须给done传入错误标识,让Dropzone移除该文件 done("Error retrieving AWS token"); vex.dialog.alert("Error retrieving AWS token") }); }
4. 增加队列卡死兜底检测
即使上述逻辑都配置完成,还是可能因为网络波动出现单上传任务永久挂起的情况,增加定时检测逻辑自动恢复队列:
// 追加到init方法内 init: function() { // 每10秒扫描一次当前正在处理的文件 setInterval(() => { const processingFiles = this.files.filter(f => f.status === 'processing'); processingFiles.forEach(file => { // 如果文件处理超过60秒没完成,判定为卡死 if (Date.now() - file.processingStartedAt > 60000) { // 中断挂死的上传实例 file.s3upload?.abort(); // 重置状态重新入队 file.status = 'queued'; this.enqueueFile(file); } }) }, 10000) }
内容的提问来源于stack exchange,提问作者Scott
相关产品推荐
相关产品推荐

