Last active
November 14, 2017 17:57
-
-
Save eschwartz/310a7d9938b60eaf49c8d56c1ccfb759 to your computer and use it in GitHub Desktop.
Load env vars from S3
This file contains 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 S3 = require('aws-sdk').S3; | |
const dotenv = require('dotenv'); | |
const Url = require('url'); | |
const p = require('util').promisify; | |
const fs = require('fs'); | |
/** | |
* @param {string} envUri | |
* @returns {Promise<Object>} | |
*/ | |
async function loadEnv(envUri) { | |
const content = await loadFromUri(envUri); | |
return dotenv.parse(content); | |
} | |
async function loadFromUri(uri) { | |
const protocol = Url.parse(uri).protocol; | |
if (protocol === 'file:') { | |
let filePath = uri.slice('file://'.length) | |
return await p(fs.readFile)(filePath, 'utf8'); | |
} | |
if (protocol === 's3:') { | |
const s3 = new S3(); | |
const s3Obj = parseS3Path(uri); | |
const res = await s3.getObject(s3Obj).promise(); | |
return res.Body.toString('utf8'); | |
} | |
throw new Error(`Unrecognized protocol: ${protocol}. Expected s3: or file:`); | |
} | |
/** | |
* @param {string} s3Path | |
* @returns {{Bucket: *, Key: *}} | |
*/ | |
function parseS3Path(s3Path) { | |
const matches = new RegExp('^s3://([^/]+)/(.+)$').exec(s3Path.trim()); | |
if (!matches) { | |
throw new Error(`Invalid s3 path: ${s3Path}`); | |
} | |
return { Bucket: matches[1], Key: matches[2] }; | |
} | |
module.exports = loadEnv; |
This file contains 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 S3 = require('aws-sdk').S3; | |
const dotenv = require('dotenv'); | |
const co = require('co'); | |
/** | |
* @param {string} s3Path | |
* @returns {Promise<Object>} | |
*/ | |
function loadS3Env(s3Path) { | |
return co(function*() { | |
const s3 = new S3(); | |
try { | |
const s3Obj = parseS3Path(s3Path); | |
const res = yield cb => s3.getObject(s3Obj, cb); | |
return dotenv.parse(res.Body.toString('utf8')); | |
} | |
catch (err) { | |
throw new Error(`Failed to load env from ${s3Path}: ${err.message}`); | |
} | |
}); | |
} | |
/** | |
* @param {string} s3Path | |
* @returns {{Bucket: *, Key: *}} | |
*/ | |
function parseS3Path(s3Path) { | |
const matches = new RegExp('^s3://([^/]+)/(.+)$').exec(s3Path.trim()); | |
if (!matches) { | |
throw new Error(`Invalid s3 path: ${s3Path}`); | |
} | |
return { Bucket: matches[1], Key: matches[2] }; | |
} | |
module.exports = loadS3Env; |
This file contains 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
import {S3} from 'aws-sdk'; | |
const dotenv = <any>require('dotenv'); | |
async function loadS3Env(s3Path:string):Promise<Object> { | |
const s3 = new S3(); | |
try { | |
const s3Obj = parseS3Path(s3Path); | |
const res = await new Promise<any>((onRes, onErr) => { | |
s3.getObject(s3Obj, (err, res) => err ? onErr(err) : onRes(res)); | |
}); | |
return dotenv.parse(res.Body.toString('utf8')); | |
} | |
catch (err) { | |
throw new Error(`Failed to load env from ${s3Path}: ${err.message}`); | |
} | |
} | |
function parseS3Path(s3Path:string):{ Bucket: string, Key: string } { | |
const matches = new RegExp('^s3://([^/]+)/(.+)$').exec(s3Path.trim()); | |
if (!matches) { | |
throw new Error(`Invalid s3 path: ${s3Path}`); | |
} | |
return { Bucket: matches[1], Key: matches[2] }; | |
} | |
export default loadS3Env; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment