Skip to content

Commit

Permalink
BRS-501: setting up upload to S3 code for export data (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltruong authored May 12, 2022
1 parent 98004d5 commit 87ae9ac
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lambda/export/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const fs = require("fs");
const writeXlsxFile = require("write-excel-file/node");
const { runScan, runQuery, TABLE_NAME } = require("../dynamoUtil");
const {
Expand All @@ -11,6 +14,7 @@ const {
} = require("../formulaUtils");

const FILE_PATH = process.env.FILE_PATH || "./";
const FILE_NAME = process.env.FILE_NAME || "A&R_Export.xlsx";

const noteKeys = {
"Frontcountry Camping": "notes_frontcountryCamping",
Expand Down Expand Up @@ -521,8 +525,15 @@ exports.handler = async (event, context) => {

await writeXlsxFile(rowsArray, {
schema,
filePath: FILE_PATH + "data.xlsx",
filePath: FILE_PATH + FILE_NAME,
});
console.log("Report generated");

// This means we are uploading to S3
if (FILE_PATH === "/tmp/" && process.env.S3_BUCKET_DATA) {
await uploadToS3();
}

console.log("=== Export successful ===");
}
} catch (err) {
Expand Down Expand Up @@ -734,3 +745,17 @@ function generateRowsArray(groupedReports) {

return rowsArray;
}

async function uploadToS3() {
// Get file as buffer
const buffer = fs.readFileSync(FILE_PATH + FILE_NAME);

const params = {
Bucket: process.env.S3_BUCKET_DATA,
Key: FILE_NAME,
Body: buffer,
};

await s3.putObject(params).promise();
console.log("File successfully uploaded to S3");
}

0 comments on commit 87ae9ac

Please sign in to comment.