res.download() throws 'request aborted' error when downloading large files

时间:2019-01-18 19:11:52

标签: node.js amazon-web-services express nginx amazon-s3

My MEAN stack app requires a feature where users can select any number of files from a list of files available on s3, which are then downloaded, zipped up, and returned to the user as a download. For extremely large files, the initial request would time out while larger files were being downloaded/zipped, so now there are 3 separate requests. The first request is a POST providing the list of files, which returns a token. Once this request returns, a second, constant polling GET request is sent using this token to determine if the download/zip is finished. This will fire once every second until it receives the desired response. Once the downloading and zipping of files is completed, a db entry is generated matching the provided token, which satisfies the second request, now the third and final request GET fires, which is a request to download the completed file.

function downloadZip( req: Request, res: Response, next: Function ) {

if ( !req.params.token ) {
    return next( new Error( "downloadZip requires token." ) );
}

icmDownloadModel.findByToken( req.params.token, ( err, data ) => {

    if ( err ) {
        return next( err );
    }

    if ( data && data.fileName && data.token ) {

        let localPath: string = path.join( __dirname, "..", "uploads", data.token + '.zip' );

        res.download( localPath, data.fileName, err => {

            if ( err ) {
                return next( err );
            }

            fs.unlink( localPath );

            icmDownloadModel.delete( data._id, ( err ) => {
                if ( err ) {
                    next( err );
                }
            });

        } );

    } else {

        next( new Error( "Unable to locate file." ) );
    }

} );

};

So far, this is working almost perfectly, however, when attempting to download files 1GB+, I get an error saying 'request aborted'. The files are successfully downloaded from s3 and zipped, but the res.download() craps out after a few seconds. I have managed to download files upwards of 250MB without issue. Also, this works fine on my local instance, but fails on the live DEV environment.

Any help would be appreciated.

0 个答案:

没有答案