Last active
September 2, 2015 06:18
-
-
Save joshgillies/482f82d41bff964c0a8b to your computer and use it in GitHub Desktop.
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
module['exports'] = function accessRequestData (hook) { | |
var AWS = require('aws-sdk') | |
var res = hook.res | |
AWS.config.update({ | |
accessKeyId: hook.env.accessKeyId, | |
secretAccessKey: hook.env.secretAccessKey, | |
region: hook.env.region | |
}) | |
var s3 = new AWS.S3({ | |
apiVersion: hook.env.s3Version, | |
params: { | |
Bucket: hook.env.s3Bucket | |
} | |
}) | |
var key = hook.params.id || 'latest.jpg' | |
console.log('Fetching: ' + key) | |
s3.headObject({ Key: key }, function (err, data) { | |
console.log(data) | |
s3.getSignedUrl('getObject', { Key: data.Metadata.ref }, function (err, url) { | |
if (err) { | |
console.log(err.message || err) | |
return res.end(err.message || err) | |
} | |
console.log('Success', url) | |
res.end(renderHtml({ url: url })) | |
}) | |
}) | |
function renderHtml (opts) { | |
var html = [ | |
'<html>', | |
' <head>', | |
' <title>Hello Mt Wellington!</title>', | |
' <style>', | |
' body { background-size: cover; margin: 0; padding: 0; }', | |
' img { width: 100%; }', | |
' </style>', | |
' </head>', | |
' <body>', | |
' <img src="' + opts.url + '" />', | |
' </body>', | |
'</html>' | |
] | |
return html.join('\n') | |
} | |
// s3.listObjects({ Marker: hook.params.id || 'latest.jpg' }, function(err, data) { | |
// res.end(JSON.stringify(data, true, 2)) | |
// return | |
// console.log('Fetching: ' + data.Contents[0].Key) | |
// s3.getObject({ Key: data.Contents[0].Key }, function (err, data) { | |
// if (err) { | |
// console.log(err.message || err) | |
// return res.end(err.message || err) | |
// } | |
// console.log('Success') | |
// res.end('<html><head></head><body><img src="data:image/jpeg;base64,' + data.Body.toString('base64') + '" /></body></html>') | |
// }) | |
// }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment