Skip to content

Commit 35b3da8

Browse files
authored
Merge pull request #284 from yoution/feature/s3download
fix: fix issue when download large file, topcoder-platform/community-…
2 parents 33e5bc0 + 7ffe022 commit 35b3da8

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/common/helper.js

+12
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,17 @@ function * downloadFile (fileURL) {
713713
return downloadedFile.Body
714714
}
715715

716+
/**
717+
* Function to create s3 readstream from given S3 URL
718+
* @param{String} fileURL S3 URL of the file to be downloaded
719+
* @returns {Object} ReadStream of downloaded file
720+
*/
721+
function createS3ReadStream (fileURL) {
722+
const { bucket, key } = AmazonS3URI(fileURL)
723+
logger.info(`create s3 readStream(): file is on S3 ${bucket} / ${key}`)
724+
return s3.getObject({ Bucket: bucket, Key: key }).createReadStream()
725+
}
726+
716727
/**
717728
* Wrapper function to post to bus api. Ensures that every event posted to bus api
718729
* is duplicated and posted to bus api again, but to a different "aggregate" topic
@@ -895,6 +906,7 @@ module.exports = {
895906
checkCreateAccess,
896907
checkGetAccess,
897908
checkReviewGetAccess,
909+
createS3ReadStream,
898910
downloadFile,
899911
postToBusApi,
900912
cleanseReviews,

src/controllers/SubmissionController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function * downloadSubmission (req, res) {
2828
fileName = `submission-${result.submission.id}.zip`
2929
}
3030
res.attachment(fileName)
31-
res.send(result.file)
31+
helper.createS3ReadStream(result.submission.url).pipe(res)
3232
}
3333

3434
/**

src/services/SubmissionService.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,14 @@ getSubmission.schema = {
164164
}
165165

166166
/**
167-
* Function to download submission from S3
167+
* Function to get submission
168168
* @param {Object} authUser Authenticated User
169169
* @param {String} submissionId ID of the Submission which need to be retrieved
170-
* @return {Object} Submission retrieved from S3
170+
* @return {Object} Submission retrieved
171171
*/
172172
function * downloadSubmission (authUser, submissionId) {
173173
const record = yield getSubmission(authUser, submissionId)
174-
const downloadedFile = yield helper.downloadFile(record.url)
175-
return { submission: record, file: downloadedFile }
174+
return { submission: record }
176175
}
177176

178177
/**

0 commit comments

Comments
 (0)