我在使用NestJS和AWS-S3开发的“中间件”(从现在起,文件服务)服务中遇到了一些超时问题。
文件服务有两个主要目的:
上传工作没有问题。
下载小文件也没有问题。
但是,当我尝试下载大型文件(> 50 to )时,几秒钟后,连接会因为超时而中断,正如您可以发现的那样,下载失败了。
我花了几天时间寻找解决方案和阅读文档。
在这里,其中一些:
但什么都不管用。
在这里,代码:
存储定义类
export class S3Storage implements StorageInterface {
config: any;
private s3;
constructor() {}
async initialize(config: S3ConfigInterface): Promise<void> {
this.config = config;
this.s3 = new AWS.S3();
// initialize S3 Configuration
AWS.config.update({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
region: config.region
});
}
async downloadFile(target: FileDto): Promise<Readable> {
const params = {
Bucket: this.config.Bucket,
Key: target.sourcePath
};
return this.s3.getObject(params).createReadStream();
}
}下载方法
private async downloadOne(target: FileDto, request, response) {
const storage = await this.provider.getStorage(target.datasource);
response.setHeader('Content-Type', mime.lookup(target.filename) || 'application/octet-stream');
response.setHeader('Content-Disposition', `filename="${path.basename(target.filename)}";`);
const stream = await storage.downloadFile(target);
stream.pipe(response);
// await download and exit
await new Promise((resolve, reject) => {
stream.on('end', () => {
resolve(`${target.filename} has been downloaded`);
});
stream.on('error', () => {
reject(`${target.filename} could not be downloaded`);
});
});
}如果有人遇到了同样的问题(或类似的)或任何人有任何想法(有用与否),我将感谢任何帮助或建议。
提前谢谢你。
发布于 2022-01-21 09:41:13
我也遇到了同样的问题,下面是如何解决这个问题的:我决定将内容下载到一个临时文件(我的API的Amazon后端服务器)中,而不是直接从S3获取该文件来处理这个文件,而是从那个临时文件中处理这个流。之后,为了不填满硬盘,我删除了临时文件。
https://stackoverflow.com/questions/54695087
复制相似问题