Created
January 16, 2018 23:09
-
-
Save zacharyhill/861eba021e782b2fa694fd0308cf2b63 to your computer and use it in GitHub Desktop.
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 AWS = require('aws-sdk'); | |
const s3 = new AWS.S3(); | |
var Bucket; | |
function bucketNameIsUndefined() { | |
if (!Bucket) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
const bucketNotSetErr = 'Bucket name has not been set'; | |
module.exports = { | |
/* | |
** | |
*/ | |
getEmailsFromKeys: function(keys) { | |
if (bucketNameIsUndefined()) { | |
throw new Error(bucketNotSetErr); | |
} | |
else { | |
const unparsedEmails = keys.map((key) => { | |
if (typeof key === 'string') { | |
return s3.getObject({ | |
Bucket, | |
Key: key, | |
}).promise().then((unparsedEmail) => { | |
console.log('unparsed email: ' + unparsedEmail); | |
unparsedEmail.AWSKey = key; | |
return unparsedEmail; | |
}); | |
} | |
else { | |
throw new Error('Key must be a string!'); | |
} | |
}); | |
return Promise.all(unparsedEmails); | |
} | |
}, | |
/* | |
** get array of all keys on aws s3 bucket | |
** each key represents an unparsed email | |
*/ | |
getKeys: function() { | |
if (bucketNameIsUndefined()) { | |
throw new Error(bucketNotSetErr); | |
} | |
const MaxKeys = 1000; | |
return s3.listObjects({ Bucket, MaxKeys}).promise() | |
.then((data) => { | |
return data.Contents.map((obj) => { | |
return obj.Key; | |
}); | |
}); | |
}, | |
/* | |
** set name of s3 bucket. this must be done | |
** before any of these methods will work | |
*/ | |
setName: function(name) { | |
Bucket = name; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment