Skip to content

Commit

Permalink
Change Lambda upload paths
Browse files Browse the repository at this point in the history
Will now upload the schema.org report to a date-delimited path, as
well as uploading to a "latest" file at the same URL (for easy
crawling).
  • Loading branch information
openfirmware committed Apr 14, 2020
1 parent 41d0d34 commit 22a3f59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ exports.handler = async (event) => {
let report = new Report(staURL)
await report.generate()

// Upload to S3
// Determine upload paths
// First is for the "latest" version of the report
// Second is for the "archived" version of the report
let date = report.getDate()
let year = date.getUTCFullYear()
let month = (date.getUTCMonth() + 1).toString()
// Add padding if necessary
month = month.length === 1 ? `0${month}` : month
let latestPath = `${process.env.S3_PATH}/latest.json`
let archivedPath = `${process.env.S3_PATH}/${year}/${month}/${report.timeString()}.json`

// Upload to S3 archived path
console.log(`Uploading to bucket ${process.env.S3_BUCKET}/${process.env.S3_PATH}/.`)
let putDoc = s3.putObject({
ACL: "public-read",
Body: report.toJSON(),
Bucket: process.env.S3_BUCKET,
ContentType: "application/json",
Key: `${process.env.S3_PATH}/${report.timeString()}.json`
Key: archivedPath
}, (err, data) => {
if (err) {
console.error(err, err.stack)
Expand All @@ -45,4 +56,21 @@ exports.handler = async (event) => {
})

await putDoc.promise()

// Copy archived version on S3 to "latest" path
let copyDoc = s3.copyObject({
ACL: "public-read",
Bucket: process.env.S3_BUCKET,
ContentType: "application/json",
CopySource: `${process.env.S3_BUCKET}/${archivedPath}`,
Key: latestPath
}, (err, data) => {
if (err) {
console.error(err, err.stack)
} else {
console.log("Finished copy to latest path.", data)
}
})

await copyDoc.promise()
}
5 changes: 5 additions & 0 deletions lib/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class Report {
}
}

// Return the Date object for when the report was generated
getDate() {
return this.reportTime
}

// Return an ISO8601 string of the time that the report was started
timeString() {
return this.reportTime.toISOString()
Expand Down

0 comments on commit 22a3f59

Please sign in to comment.