我有一个包含文件的 AWS s3 存储桶。当我提供文件 ID 时,我想下载 zip 中的文件。
我使用nodejs作为后端,nextjs作为前端如何做到这一点。
目前在后端它正在工作,但是当在前端下载大文件时,它会说超时错误。当前后端的流程是文件进入后端,然后压缩并发送到前端下载。
我增加了服务器配置中的计时,但没有任何改进。
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.responseType = 'blob';
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('x-access-token', `${createJWTToken()}`);
xhr.send(JSON.stringify(dataBody));
xhr.onprogress = function (e) {
if (e.lengthComputable) {
const progress = (e.loaded / e.total) * 100;
setProgress(Math.ceil(progress));
}
};
xhr.onload = function (e) {
if (this.status === 200) {
const blob = new Blob([this.response], { type: 'application/zip' });
const urls = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = urls;
link.setAttribute('download', `FileDrop-${Date.now()}.zip`);
document.body.appendChild(link);
link.click();
link.remove();
}
};
我想下载大尺寸的文件。
根据您的描述,您可以尝试将客户端
xhr
超时值设置为零。
xhr.timeout = 0
考虑到您使用
aws-sdk
包在 Node.js 后端上使用了 AWS s3。您可以按如下所示配置设置:
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
httpOptions: {
timeout: 600000, // Timeout in milliseconds (10 minutes)
connectTimeout: 10000 // Connection timeout (10 seconds)
}
});
这里设置的超时值是
10 minutes
,您可以根据自己的喜好增加。
希望我能帮上忙