Last active
May 17, 2024 12:04
-
-
Save louh/a55560560fedae99dbcd61aa0836874f to your computer and use it in GitHub Desktop.
AWS Lambda saveWeekenderDataToS3()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const http = require('http'); | |
const BUCKET_ID = 'weekender-data'; | |
const FILES = { | |
'weekendstatus.js': 'http://web.mta.info/status/weekendstatus.js', | |
'weekendroutestatus.js': 'http://web.mta.info/status/weekendroutestatus.js', | |
'weekendboroughstatus.js': 'http://web.mta.info/status/weekendboroughstatus.js', | |
'LinesStaticData.js': 'http://web.mta.info/weekender/Data/LinesStaticData.js' | |
}; | |
exports.handler = async (event) => { | |
const responses = await getAll(FILES); | |
return responses; | |
}; | |
function getAll (obj) { | |
return Promise.all(Object.entries(obj).map(getAndSaveToS3)); | |
} | |
async function getAndSaveToS3 ([key, url] = entry) { | |
const response = await getContent(url); | |
const etag = await putObjectToS3(BUCKET_ID, key, response); | |
return etag; | |
} | |
function getContent (url) { | |
// return new pending promise | |
return new Promise((resolve, reject) => { | |
// select http or https module, depending on reqested url | |
// const lib = url.startsWith('https') ? require('https') : require('http'); | |
const request = http.get(url, (response) => { | |
// handle http errors | |
if (response.statusCode < 200 || response.statusCode > 299) { | |
reject(new Error('Failed to load page, status code: ' + response.statusCode)); | |
} | |
// temporary data holder | |
const body = []; | |
// on every content chunk, push it to the data array | |
response.on('data', (chunk) => body.push(chunk)); | |
// we are done, resolve promise with those joined chunks | |
response.on('end', () => resolve(body.join(''))); | |
}); | |
// handle connection errors of the request | |
request.on('error', (err) => reject(err)); | |
}); | |
} | |
function putObjectToS3 (bucket, key, data) { | |
// return new pending promise | |
return new Promise((resolve, reject) => { | |
const s3 = new AWS.S3(); | |
const params = { | |
Bucket: bucket, | |
Key: key, | |
Body: data, | |
ACL: 'public-read' // Make sure IAM role has S3:PutObjectACL permissions set | |
}; | |
s3.putObject(params, function (err, data) { | |
if (err) { | |
reject(new Error(err)); | |
} else { | |
resolve(data); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment