Skip to content

Commit

Permalink
Merge pull request #8 from jrjohnson/write-whole-file
Browse files Browse the repository at this point in the history
Finish writing all data
  • Loading branch information
jrjohnson authored Mar 30, 2020
2 parents 3b33e41 + c0442be commit 942a946
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
21 changes: 9 additions & 12 deletions handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ module.exports.storeSurvey = async event => {
destinationStream
);

return new Promise(resolve => {
console.log(`survey data extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}.csv`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}.csv`,
Body: fs.createReadStream(fileName)
};
s3.upload(params).promise().then(() => {
console.log('done!');
resolve({ message: `Stored ${surveyId}`, event });
});
});
console.log(`survey data extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}.csv`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}.csv`,
Body: fs.createReadStream(fileName)
};
await s3.upload(params).promise();
console.log('done!');
return { message: `Stored ${surveyId}`, event };
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ getSurveyResults(
surveyId,
destinationStream
).then(() => {
console.log("done\n\n");
console.log("done\n");
});
7 changes: 6 additions & 1 deletion src/get-survey-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ async function writeFile(token, url, destinationStream) {
const entry = await zipFile.readEntry();
const readStream = await zipFile.openReadStream(entry);
readStream.pipe(destinationStream);
return new Promise(resolve => {
destinationStream.on("end", () => {
resolve();
});
});
}

async function getSurveyResults(token, dataCenter, surveyId, destinationStream) {
const progressId = await requestResultsToBeBuilt(token, dataCenter, surveyId);
const fileUrl = await waitForBuildToComplete(token, dataCenter, progressId);
return writeFile(token, fileUrl, destinationStream);
return await writeFile(token, fileUrl, destinationStream);
}

exports.getSurveyResults = getSurveyResults;

0 comments on commit 942a946

Please sign in to comment.